Azu Posted September 12, 2007 Share Posted September 12, 2007 Hello I'm used to using str_replace but now I want to switch to a regex solution since str_replace isn't working how I want it to work. Can somebody please tell me how to use a regex function to act like str_replace, except without changing the capilization of letters? For example, stri_replace('a test','<b>A test</b>','This is a test'); outputs '<b>A test</b>' (it changed the lowercase a to an upper case A). How do I do it with a regex function so that it won't change the case? This is for a search script that I am making, and have it change instances of the search phrase in the result to bold. It isn't supposed to change the case though. Please help.. I've never used regex before except copy and pasting regex that I found by searching, but I can't find this by searching.. x_x Quote Link to comment Share on other sites More sharing options...
hackerkts Posted September 12, 2007 Share Posted September 12, 2007 <?php $text = "hello, how are you?"; $search = $_GET['search']; $text = str_replace($search, '<b>'.$search.'</b>', $text); echo $text; ?> If your mixed replace don't put capital then it wouldn't change to capital letter. Quote Link to comment Share on other sites More sharing options...
Azu Posted September 12, 2007 Author Share Posted September 12, 2007 Thanks, this is how I am doing it right now. The problem is that it changes the case in the results to the case in the search. Quote Link to comment Share on other sites More sharing options...
xyn Posted September 12, 2007 Share Posted September 12, 2007 for php 5 it's str_ireplace("term", "replace", $string); Quote Link to comment Share on other sites More sharing options...
Azu Posted September 12, 2007 Author Share Posted September 12, 2007 Thanks, this is how I am doing it right now. The problem is that it changes the case in the results to the case in the search. Quote Link to comment Share on other sites More sharing options...
xyn Posted September 12, 2007 Share Posted September 12, 2007 So you basically want your SQL syntax lowercase? to match the keywords :S i'm confused. sorry. Quote Link to comment Share on other sites More sharing options...
effigy Posted September 12, 2007 Share Posted September 12, 2007 <?php $tests = array( 'i am a string', 'I AM A STRING' ); foreach ($tests as $test) { echo $test, ' => ', preg_replace('/\b(am)\b/i', '<b>\1</b>', $test), '<br>'; } ?> Quote Link to comment Share on other sites More sharing options...
Azu Posted September 12, 2007 Author Share Posted September 12, 2007 Thank you effigy! That's exactly what I was looking for Quote Link to comment 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.