Jump to content


str,preg,eregi _replace


2 replies to this topic

#1 Stu

    Retired P2L Staff

  • Publishing Betazoids
  • PipPipPipPip
  • 1,761 posts
  • Gender:Male

Posted 25 January 2006 - 06:36 AM

hello,

im making a search feature and its all working fine at the moment, the only problem i am having, is when my search comes through i have the particular search word highlighted in orange on the results, thats fine, BUT i was hoping it would preserve the case.

for example when i search for vray it changes all of them to vray instead of VRay. so id just like it too preserve the case.

at first i used str_replace, but it is case sensitive so it didnt show all the relevant searches highlighted in orange, and now im using eregi_replace so its catching the ones with uppercase and lowercase letters. i know its possible to just convert the first letter to uppercase, but i used VRay as an example above as it has 2 upper case letters to start.

would some regex do the trick? if so how would you go about doing that?

heres my relevant code:

 while($row = mysql_fetch_object($result_portfolio))
{

$string_p_1 = "<table style=\"width: 100%; text-align: center; border-top: 1px dashed #EFEFEF;\"><tr><td width=\"100px\" height=\"100px\" style=\"text-align: left;\"><a href=\"$row->image_url\" target=\"_BLANK\"><img src=\"$row->thumb_url\" border=\"2\" height=\"90px\" width=\"90px\" alt=\"$row->title\" /></a></td>";
		
$string_p_2 = "<td style=\"text-align: left;\"><b>Title:</b> $row->title<br /><b>Description:</b> $row->description<br /><b>Category:</b> $row->category<br /><i>Submitted On $row->date</i></td></tr>
</table>
<br />";

$str_string_p = eregi_replace($search_item, '<font color=orange>'.$search_item.'</font>', $string_p_2);
		
echo "$string_p_1 $str_string_p";

}

}
else{
echo "<h1>No Results from Porfolio</h1>";
}

sorry about the lack of indenting, it seems to have dissappeared in here... *note too self: use spaces not tabs* ^_^

if you want to search for VRay to see what i mean, look in my portfolio @ the portfolio page and notice how some entries in the 3D section that have vray in them are spelt as VRay (example is 3/4 down named 'The Arches'), then use the search function and search vray and notice that they are all changed to vray.

any help most appreciated

Edited by Stu, 25 January 2006 - 07:05 AM.


#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 25 January 2006 - 11:29 AM

$str_string_p = preg_replace('#('.$search_item.')#i', '<span style="color:orange;">\\1</span>', $string_p_2);
I don't know if ereg_replace can do anything like what's above, but i believe preg_replace is faster either way.
Your script was searching for a specific string, and replacing it with the exact string, only colored orange. This uses a back-reference to color the exact string match orange (no matter what case it is in).

Edited by rc69, 25 January 2006 - 11:29 AM.


#3 Stu

    Retired P2L Staff

  • Publishing Betazoids
  • PipPipPipPip
  • 1,761 posts
  • Gender:Male

Posted 25 January 2006 - 12:07 PM

worked perfectly as usual, thanks alot :(





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users