[PHP] Re: Battling with highlighting search criteria
I have a search string ($search) and a result ($result). I need to highlight every occurance of $search in $result. I know that it means that I need to change (for example) $result = This is a test, isn't it? $search = is Into $result = thspan class=highlightis/span span class=highlightis/span a test, span class=highlightis/span'nt it Now I can easily see how to change the FIRST occurrence of the word, but how can I change every occurance? How about str_replace? $result = This is a test, isn't it?; $search = is; $replacement = 'span class=highlight' . $search . '/span'; $result = str_replace( $search, $replacement, $result ); HTH Erwin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Re: Battling with highlighting search criteria
Hey there I told you my brain was feeling fuzzy :) This works great. Only one problem... I would like it to be case insensitive... Any way? I assume that it would be a ereg/preg replace, but I have no clue with regexp at all. Anyone who could help?? David Russell IT Support Manager Barloworld Optimus (Pty) Ltd Tel: +2711 444-7250 Fax: +2711 444-7256 e-mail: [EMAIL PROTECTED] web: www.BarloworldOptimus.com -Original Message- From: Erwin [mailto:erwin;isiz.com] Sent: 04 November 2002 10:42 AM To: [EMAIL PROTECTED] Subject: [PHP] Re: Battling with highlighting search criteria I have a search string ($search) and a result ($result). I need to highlight every occurance of $search in $result. I know that it means that I need to change (for example) $result = This is a test, isn't it? $search = is Into $result = thspan class=highlightis/span span class=highlightis/span a test, span class=highlightis/span'nt it Now I can easily see how to change the FIRST occurrence of the word, but how can I change every occurance? How about str_replace? $result = This is a test, isn't it?; $search = is; $replacement = 'span class=highlight' . $search . '/span'; $result = str_replace( $search, $replacement, $result ); HTH Erwin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php smime.p7s Description: application/pkcs7-signature
Re: [PHP] Re: Battling with highlighting search criteria
David Russell wrote: Hey there I told you my brain was feeling fuzzy :) This works great. Only one problem... I would like it to be case insensitive... Any way? I assume that it would be a ereg/preg replace, but I have no clue with regexp at all. Try the following one: $result = This is a test, isn't it?; $search = is; $result = preg_replace( '/(' . $search . ')/i', span class=\highlight\\$1/span, $result ); Grtz Erwin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Re: Battling with highlighting search criteria
Hi Erwin, Yep, this does exactly what str_replace does. How can I make the whole thing case insensitive: $result= This Is A Test, Isn't It? $search= IS It will include the 'is' in 'This', and also the 'Is' and the 'is' in 'isn't'. cheers David Russell IT Support Manager Barloworld Optimus (Pty) Ltd Tel: +2711 444-7250 Fax: +2711 444-7256 e-mail: [EMAIL PROTECTED] web: www.BarloworldOptimus.com -Original Message- From: Erwin [mailto:erwin;isiz.com] Sent: 04 November 2002 11:27 AM To: [EMAIL PROTECTED] Subject: Re: [PHP] Re: Battling with highlighting search criteria David Russell wrote: Hey there I told you my brain was feeling fuzzy :) This works great. Only one problem... I would like it to be case insensitive... Any way? I assume that it would be a ereg/preg replace, but I have no clue with regexp at all. Try the following one: $result = This is a test, isn't it?; $search = is; $result = preg_replace( '/(' . $search . ')/i', span class=\highlight\\$1/span, $result ); Grtz Erwin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php smime.p7s Description: application/pkcs7-signature
RE: [PHP] Re: Battling with highlighting search criteria
Sorry, my mistake... This is what happens when you have 2 machines - one production and one development. You get confused between the two grin Just one more thing, How can I get the return in the same case as it was originally? Ie if I have $string = Forward , and $search = for, I want spanForward/span, not span...forward/span. David Russell IT Support Manager Barloworld Optimus (Pty) Ltd Tel: +2711 444-7250 Fax: +2711 444-7256 e-mail: [EMAIL PROTECTED] web: www.BarloworldOptimus.com -Original Message- From: David Russell [mailto:DavidR;BarloworldOptimus.com] Sent: 04 November 2002 11:54 AM To: 'Erwin'; [EMAIL PROTECTED] Subject: RE: [PHP] Re: Battling with highlighting search criteria Hi Erwin, Yep, this does exactly what str_replace does. How can I make the whole thing case insensitive: $result= This Is A Test, Isn't It? $search= IS It will include the 'is' in 'This', and also the 'Is' and the 'is' in 'isn't'. cheers David Russell IT Support Manager Barloworld Optimus (Pty) Ltd Tel: +2711 444-7250 Fax: +2711 444-7256 e-mail: [EMAIL PROTECTED] web: www.BarloworldOptimus.com -Original Message- From: Erwin [mailto:erwin;isiz.com] Sent: 04 November 2002 11:27 AM To: [EMAIL PROTECTED] Subject: Re: [PHP] Re: Battling with highlighting search criteria David Russell wrote: Hey there I told you my brain was feeling fuzzy :) This works great. Only one problem... I would like it to be case insensitive... Any way? I assume that it would be a ereg/preg replace, but I have no clue with regexp at all. Try the following one: $result = This is a test, isn't it?; $search = is; $result = preg_replace( '/(' . $search . ')/i', span class=\highlight\\$1/span, $result ); Grtz Erwin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php smime.p7s Description: application/pkcs7-signature
Re: [PHP] Re: Battling with highlighting search criteria
Try the following one: $result = This is a test, isn't it?; $search = is; $result = preg_replace( '/(' . $search . ')/i', span class=\highlight\\$1/span, $result ); Grtz Erwin David Russell wrote: Hi Erwin, Yep, this does exactly what str_replace does. No, it doens't...this one is case insensitive. The pattern (which is /(is)/i in this example), has the i modifier (the last i), which means case insensitive. In fact, the above regexp even preserves case for you! How can I make the whole thing case insensitive: It already is, please try it again (I've doublechecked it by now) ;-)) Grtz Erwin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Battling with highlighting search criteria
How can I get the return in the same case as it was originally? Ie if I have $string = Forward , and $search = for, I want spanForward/span, not span...forward/span. Read my reply to your previous reply ;-)) But...some more explanation: $result = preg_replace( '/(' . $search . ')/i', span class=\highlight\\$1/span, $result ); The first argument is the pattern, which is '/(for)/i' in the case of searching for for. That will match every occurence of for, For, fOr, and so on. The case insensitivy is reached by the i modifier at the end of the pattern. The second argument is the replace string. The $1 is an backward reference to the first item between brackets. That'll mean that the from the word Forward, the $1 parameter will contain For. So, the For -part from Forward will be replaced with span class=highlightFor/span and that's exactly what you want (right?)... Hope this clears your brain up ;-))) Grtz Erwin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Re: Battling with highlighting search criteria
Hi Erwin, Great thanx, my brain is cleared grin. Well, a little more... Where would I be able to find more info about regexp's? TIA David Russell IT Support Manager Barloworld Optimus (Pty) Ltd Tel: +2711 444-7250 Fax: +2711 444-7256 e-mail: [EMAIL PROTECTED] web: www.BarloworldOptimus.com -Original Message- From: Erwin [mailto:erwin;isiz.com] Sent: 04 November 2002 12:08 PM To: [EMAIL PROTECTED] Subject: Re: [PHP] Re: Battling with highlighting search criteria How can I get the return in the same case as it was originally? Ie if I have $string = Forward , and $search = for, I want spanForward/span, not span...forward/span. Read my reply to your previous reply ;-)) But...some more explanation: $result = preg_replace( '/(' . $search . ')/i', span class=\highlight\\$1/span, $result ); The first argument is the pattern, which is '/(for)/i' in the case of searching for for. That will match every occurence of for, For, fOr, and so on. The case insensitivy is reached by the i modifier at the end of the pattern. The second argument is the replace string. The $1 is an backward reference to the first item between brackets. That'll mean that the from the word Forward, the $1 parameter will contain For. So, the For -part from Forward will be replaced with span class=highlightFor/span and that's exactly what you want (right?)... Hope this clears your brain up ;-))) Grtz Erwin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php smime.p7s Description: application/pkcs7-signature