note 86099 added to function.str-ireplace

From: Date: Fri, 03 Oct 2008 02:29:43 +0000
Subject: note 86099 added to function.str-ireplace
Groups: php.notes 
Request: Send a blank email to php-notes+get-145003@lists.php.net to get a copy of this message
If you want to keep the original capitalization when replacing some text (e.g. for highlighting the search-string in the the search result), you can use this code I wrote: <?php function ext_str_ireplace($findme, $replacewith, $subject) { // Replaces $findme in $subject with $replacewith // Ignores the case and do keep the original capitalization by using $1 in $replacewith // Required: PHP 5 return substr($subject, 0, stripos($subject, $findme)). str_replace('$1', substr($subject, stripos($subject, $findme), strlen($findme)), $replacewith). substr($subject, stripos($subject, $findme)+strlen($findme)); } ?> Example for using: <?php $search = 'replaceme'; $text = 'Please RePlaCeMe, OK?'; // Will print "RePlaCeMe" with red color, but after this it would be "replaceme", not "RePlaCeMe" $replace = '<font color="#FF0000">'.$search.'</font>'; echo str_ireplace($search, $replace, $text); // Will print "RePlaCeMe" with red color $replace = '<font color="#FF0000">$1</font>'; echo ext_str_ireplace($search, $replace, $text); ?> ---- Server IP: 203.88.112.190 Probable Submitter: 87.165.206.183 ---- Manual Page -- http://www.php.net/manual/en/function.str-ireplace.php Edit -- https://master.php.net/note/edit/86099 Del: integrated -- https://master.php.net/note/delete/86099/integrated Del: useless -- https://master.php.net/note/delete/86099/useless Del: bad code -- https://master.php.net/note/delete/86099/bad+code Del: spam -- https://master.php.net/note/delete/86099/spam Del: non-english -- https://master.php.net/note/delete/86099/non-english Del: in docs -- https://master.php.net/note/delete/86099/in+docs Del: other reasons-- https://master.php.net/note/delete/86099 Reject -- https://master.php.net/note/reject/86099 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#145003) next »