1

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);
Tike
  • 387
  • 1
  • 2
  • 10

2 Answers2

1

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.

Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
  • I am aware of the problems with regex and html. But it's taken from a phpbb database so the structure is always consistent. – Tike Nov 28 '11 at 16:26
0

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.

morja
  • 8,297
  • 2
  • 39
  • 59