homepage Welcome to WebmasterWorld Guest from 54.161.214.221
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
eregi replace to match keywords ONLY between > and <
el_roboto




msg:3562724
 11:29 am on Jan 31, 2008 (gmt 0)

Working on a function to automatically bold keywords in the page content, one of my concerns is that if the keywords are used within tags that the code is going to get messed up.

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.

 

PHP_Chimp




msg:3563208
 9:12 pm on Jan 31, 2008 (gmt 0)

'%(?:<[\w\s"']+>)([^<]+)(?:</[\w]+>)%is';
Should work for you as the (?: will stop the start and end tags from getting captured into sub-patterns. So hopefully only the contents of the div in your example will be captured.

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]

el_roboto




msg:3563805
 1:20 pm on Feb 1, 2008 (gmt 0)

Thanks for the response

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]

el_roboto




msg:3572766
 1:32 pm on Feb 12, 2008 (gmt 0)

been testing this function for a while now and there are a few issues.

If a keyword listed in the meta data in lower case, and then the word is found in the content with a capital letter or as capitals, then it is replaced with a lower case version.

Anyone have any suggestions to overcome this?

eelixduppy




msg:3573099
 6:36 pm on Feb 12, 2008 (gmt 0)

You are going to have to make the replacement with what was already found in the string. So you have to change your pattern and your replacement string to do just that. Try the following:

$pattern = "/(?!<.*?)(".preg_quote($key,'/').")(?![^<>]*?>)/si";
$replacement = "<b>\\2</b>";
$pagecontent = preg_replace($pattern,$replacement,$pagecontent);

Should work, although its untested.

el_roboto




msg:3577826
 12:30 pm on Feb 18, 2008 (gmt 0)

thanks for the response!

just got round to testing and finding that the keywords picked up are replaced with <b></b>

eelixduppy




msg:3578114
 6:57 pm on Feb 18, 2008 (gmt 0)

I'm sorry. The replacement should be the following:

$replacement = "<b>\\1</b>";

I had '2' instead of '1' so it was actually replacing with the wrong grouped string.

el_roboto




msg:3578623
 9:17 am on Feb 19, 2008 (gmt 0)

sorry I should have noticed that! d'oh

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved