Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6865117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:00:00+00:00 2026-05-27T03:00:00+00:00

I have this replace regex (it’s taken from the phpbb source code). $match =

  • 0

I have this replace regex (it’s taken from the phpbb source code).

$match = array(
                '#<!\-\- ([mw]) \-\-><a (?:class="[\w-]+" )?href="(.*?)" target\=\"_blank\">.*?</a><!\-\- \1 \-\->#',
                '#<!\-\- .*? \-\->#s',
                '#<.*?>#s',
            );
$replace = array( '\2',  '', '');

$message = preg_replace($match, $replace, $message);

If I run it through a message like this

asdfafdsfdfdsfds
<!-- m --><a class="postlink" href="http://website.com/link-is-looooooong.txt">http://website.com/link ... oooong.txt</a><!-- m -->
asdfafdsfdfdsfds4324

It returns this

asdfafdsfdfdsfds
http://website.com/link ... oooong.txt
asdfafdsfdfdsfds4324

However I would like to make it into a replace function. So I can replace the link title in a block by providing the href.

I want to provide the url, new url and new title. So I can run a regex with these variables.

$url = 'http://website.com/link-is-looooooong.txt';
$new_title = 'hello';
$new_url = 'http://otherwebsite.com/';

And it would return the same raw message but with the link changed.

<!-- m --><a class="postlink" href="http://otherwebsite.com/">hello</a><!-- m -->

I’ve tried tweaking it into something like this but I can’t get it right. I don’t know how to build up the matched result so it has the same format after replacing.

$message = preg_replace('#<!\-\- ([mw]) \-\-><a (?:class="[\w-]+" )?href="'.preg_quote($url).'" target\=\"_blank\">(.*?)</a><!\-\- \1 \-\->#', $replace, $message);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:00:01+00:00Added an answer on May 27, 2026 at 3:00 am

    You need to catch the other parts in groups as well and then use them in the replacement. try something like this:

    $replace = '\1http://otherwebsite.com/\3hello\4';
    $reg = '#(<!-- ([mw]) --><a (?:class="[\w-]+" )?href=")'.preg_quote($url).'("(?: target="_blank")?>).*?(</a><!-- \2 -->)#';
    $message = preg_replace($reg, $replace, $message);
    

    See here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this regex/javascript code: toReturn = toReturn.replace(/(\[url=)(.+)(\])(.+)(\[\/url\])/g, '<a target=\_blank\ href=\$2>\$4</a>'); But the problem
I have a WordPress blog with numerous URL's I wish to replace from this:
I have this statement: String cap = Regex.Replace(winCaption, @[^\w\.@-], ); that transforms Hello |
Assume I have this: Regex.Replace(aa cc bbbb,aa cc,,RegexOptions.IgnoreCase); But I also need to ignore
I have this simple regex replace based routine, is there anyway to improve its
I have this code, and I want to know, if I can replace only
I have this regex in JavaScript: var val = val.replace(/[:)]/g, <img src = \/img/smile.png\
i have this little class to make a multiple replace on a string: import
I have this code regex, which should transform all kind of diffrent urls into
I have code like this string pattern = <(.|\n)+?>; System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Reg(pattern);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.