简体   繁体   中英

PHP regular expression to replace links

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);

You'll find that parsing HTML with regex can be a pain and get very complex. Your best bet is to use a DOM parser, like this one , and modify the links with that instead.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM