Forum Moderators: coopster

Message Too Old, No Replies

Regex to replace matches but not links

Regex to replace matches but not links

         

tohir

8:34 am on Nov 15, 2004 (gmt 0)

10+ Year Member



I'm busy with a glossary parser and need to link to text that appears in the database

The regex I have does not fully cater for this. Could you please help me!

Regex is : (?!<a)(\bphp\b)(?!</a>)


$text = '<a href="http://www.php.net">php</a> is a web programming language . A popular php program is phpmyadmin. See http://www.php.net.';

$pattern = '/(?!<a)(\\bphp\\b)(?!<\/a>)/';

$replacement = '<strong>php</strong>';

echo preg_replace($pattern, $replacement, $text ); 

BOLD Text is results:

<a href="http://www.php.net">php</a> is a web programming language . A popular php program is phpmyadmin. See [[b]php[...]

As you can see, it does <a href="match">butdotmatch</a>

What can I do. Its not supposed to match the inside link. regex is wrecking my brain

StupidScript

9:30 pm on Nov 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome, tohir.

Try this:

$pattern = '#(?!<.*?)(\\bphp\\b)(?![^<>]*?>)#';

To include a variable (like a search term):

$lookfor = "php";

$pattern = '#(?!<.*?)(\\b'.$lookfor.'\\b)(?![^<>]*?>)#';

tohir

7:34 am on Nov 16, 2004 (gmt 0)

10+ Year Member



Thanx, it worked like a bomb!

I've modified the regexp to be case insensitive, and also not to catch text between <a>tags</a>.

Below is the final version for future users.

I've captured matches in a array, removed duplicates, and then do the replacing.

Once again, thanx alot.

<?php

// Text
$text = '<a href="http://www.php.net">php</a> is a web programming language . A popular pHp program is phpmyadmin. See [php.net.';...]

// Word to search for
$lookfor = 'php';

// regexp pattern
$pattern = '#(?!<.*?)(?!<a)(\\b'.$lookfor.'\\b)(?!<\/a>)(?![^<>]*?>)#i';
// (?!<a) (?!<\/a>) - prevents text in between <a></a>
// i (at the end) - case insensitive search

// store all matches in an array
preg_match_all ($pattern, $text, $matches);

// remove duplicate entries
$matches=array_unique($matches[0]);

// Loop to Replace entries - case sensitive
foreach ($matches as $match)
{
$pattern = '#(?!<.*?)(?!<a)(\\b'.$match.'\\b)(?!<\/a>)(?![^<>]*?>)#';

$replacement = '<a href="http://linksomewhere"><span style="background: yellow">'.$match.'</span></a>';

$text = preg_replace($pattern, $replacement, $text );
}

echo $text;

?>