phpSensei Posted January 4, 2008 Share Posted January 4, 2008 I am trying to highlight a search term, but i am a big noob in regex, and SEO.. Code <style type="text/css"> <!-- .colored { font-family: Arial, Helvetica, sans-serif; font-size: 24px; } --> </style> <?php $search = 'a'; $body = 'apple'; $pattern = '(>[^<]*)('. quotemeta($search) .')'; $replacement = '\\1<span class="colored">\\2</span>'; $body = eregi_replace($pattern, $replacement, $body); echo $body; ?> OUTPUT apple As you can see nothing is changed in the text. Quote Link to comment https://forums.phpfreaks.com/topic/84550-solved-search-term/ Share on other sites More sharing options...
GingerRobot Posted January 4, 2008 Share Posted January 4, 2008 Not sure what you were trying to achieve with the beggining of your search pattern. Try: <style type="text/css"> <!-- .colored { font-family: Arial, Helvetica, sans-serif; font-size: 24px; } --> </style> <?php $search = 'a'; $body = 'apple'; $pattern = '('.quotemeta($search).')'; $replacement = '<span class="colored">\\1</span>'; $body = eregi_replace($pattern, $replacement, $body); echo $body; ?> Quote Link to comment https://forums.phpfreaks.com/topic/84550-solved-search-term/#findComment-430777 Share on other sites More sharing options...
hitman6003 Posted January 4, 2008 Share Posted January 4, 2008 just use str_replace: <?php $search = 'a'; $body = 'apple'; $body = str_replace($search, '<span class="colored">' . $search . '</span>', $body); echo $body; ?> Quote Link to comment https://forums.phpfreaks.com/topic/84550-solved-search-term/#findComment-430779 Share on other sites More sharing options...
phpSensei Posted January 4, 2008 Author Share Posted January 4, 2008 just use str_replace: <?php $search = 'a'; $body = 'apple'; $body = str_replace($search, '<span class="colored">' . $search . '</span>', $body); echo $body; ?> Its what i did as you can see $bod = $row['job_title']; echo str_replace($keyword,'<span class="colored">'.$keyword.'</span>',$bod) .'<br>'; But i thought i couldnt achieve this properly with str_replace. It works though.. Both ways work, thankyou Quote Link to comment https://forums.phpfreaks.com/topic/84550-solved-search-term/#findComment-430785 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.