Jump to content

[SOLVED] Search Term...


phpSensei

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/84550-solved-search-term/
Share on other sites

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;

?>

Link to comment
https://forums.phpfreaks.com/topic/84550-solved-search-term/#findComment-430777
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/84550-solved-search-term/#findComment-430785
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.