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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.