Forum Moderators: coopster
ie
<div class"MZrecycling"> blah blah recycling blah</div>
becoming
<div class"MZ<b>recycling</b>"> blah blah <b>recycling</b> blah</div>
regex is not my strong point so I'm hoping someone might be able to point me in the right direction to achieve this selective replacement.
The above is for preg not ereg, so if you are using eregi then you can get rid of the i just after the final %.
The first set of ()'s will capture a < with any number of letters, _ or space characters followed by another >. So this should match a start tag.
The [^<]+ will match any number of characters that are not a <. So this should match anything other than a end tag as this will start with <.
The final part will match an end tag.
You may well need to adjust this if you are going to be using this on tags with other tags inside them. However the pattern should get you started.
[edited by: eelixduppy at 11:02 pm (utc) on Jan. 31, 2008]
[edit reason] disabled smileys [/edit]
I played around with that suggestion for some time with no good results
After many searches and lots of testing I seem to have a function which is working OK
$keys = str_replace(', ',',',$PagesKeywords);
$keys = explode(',',$keys);
$keylist = array();
foreach($keys as $key){
if(!$key ==''){ if(!in_array($key,$keylist)){
$pagecontent = preg_replace("'(?!<.*?)$key(?![^<>]*?>)'si",'<b>'.$key.'</b>',$pagecontent);
$keylist[] = $key;
}}
}
[edited by: eelixduppy at 1:26 pm (utc) on Feb. 1, 2008]
[edit reason] disabled smileys [/edit]
$pattern = "/(?!<.*?)(".preg_quote($key,'/').")(?![^<>]*?>)/si";
$replacement = "<b>\\2</b>";
$pagecontent = preg_replace($pattern,$replacement,$pagecontent);
Should work, although its untested.
$replacement = "<b>\\1</b>";
I had '2' instead of '1' so it was actually replacing with the wrong grouped string.