Can you not use str_replace, from the PHP manual :-
Note: This function is case-sensitive. Use str_ireplace() for case-insensitive replace.
Use MODx, or the cat gets it!
AHHP: I don’t see why the regex shouldn’t work, could you dump some debug info (the contents of $pattern, $replacement and $rc before preg_replace?
shamblett:
AHHP wants to replace php regardless of case with <span class="highlight">php</span> with the same case.
str_replace would require you to write all "cases" of php:
PHP, PHp, PhP, Php, pHP, pHp, phP and php
(for a N letter word there are 2^N combinations...)
MODx snippet-glossary 101:
Ditto = Content Lister -- Wayfinder == Menu Builder -- Jot = Comment Control
In my ajaxsearch config I have this:
if (!function_exists(’stripOtherTags’))
{
function stripOtherTags($text){
// Regular expressions to remove +something+
$modRegExArray[] = ’~\+(.*?)\+~’; // +phx+
// Regular expressions to remove some bad words
$badWords = array(’pagetitle’,’longtitle’,’getfield’,’GetField’,’Getfield’,’getField’); // any bad words you want
foreach($badWords as $bw) $modRegExArray[] = ’~’.$bw.’~’;
// Remove modx sensitive tags
foreach ($modRegExArray as $mReg)$text = preg_replace($mReg,’’,$text);
return $text;
}
}
Is there any way I can get this case insensitive? Ive tried playing with: preg_replace($mReg,’’,$text); to add a
/i :P
SORRY FOR CHANGING TOPIC. Just answer this anyone seeing it obvious...
Sweeet! Thank you!!
I suggest that you show the output I suggested. I’m sure there is something wrong in one of the variables if it doesn’t work. ;-)
MODx snippet-glossary 101:
Ditto = Content Lister -- Wayfinder == Menu Builder -- Jot = Comment Control
Quote from: AHHP at Dec 10, 2008, 03:11 AM
Hi,
I need to find a word and put it between two <span> tags. I did it with str_ireplace but it will miss the original case of word. for example if the word is "PHP" and there is "php" in text, "PHP" will replaced with "php"!
If you’re sure there’s only one replacement to be made, this will do it (pc is for "preserve case"):
$rc = pc_add_tag($_POST['highlight'],$rc,'<span class="highlight">','</span>');
return $rc;
function pc_add_tag($findMe, $text, $beforeTag, $afterTag) {
/* The long version, so you can see what it's doing.
$pos = stripos($text,$findMe);
$len = strlen($findMe);
$original = substr($text, $pos, $len);
$rText = $beforeTag . $original . $afterTag ;
return str_replace($original,$rText,$text);
*/
return(str_replace(substr($text, stripos($text,$findMe), strlen($findMe)),$beforeTag . substr($text, stripos($text,$findMe), strlen($findMe)) . $afterTag, $text));
}
This requires PHP5 (but could be modified for PHP4). If you might have more than one word to highlight in the selection, it gets more complicated.