Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : String Functions : str_ireplace

str_ireplace

Case-insensitive version of str_replace. (PHP 5)
mixed str_ireplace ( mixed search, mixed replace, mixed subject [, int &count] )

This function returns a string or an array with all occurrences of search in subject (ignoring case) replaced with the given replace value. If you don't need fancy replacing rules, you should generally use this function instead of eregi_replace() or preg_replace() with the i modifier.

Parameters

search
Note:

Every replacement with search array is performed on the result of previous replacement.

replace
subject

If subject is an array, then the search and replace is performed with every entry of subject, and the return value is an array as well.

count

The number of matched and replaced needles will be returned in count which is passed by reference.

If search and replace are arrays, then str_ireplace() takes a value from each array and uses them to do search and replace on subject. If replace has fewer values than search, then an empty string is used for the rest of replacement values. If search is an array and replace is a string; then this replacement string is used for every value of search.

Return Values

Returns a string or an array of replacements.

ChangeLog

Version Description
5.0.0 The count parameter was added.

Examples

Example 2443. str_ireplace() example

<?php
$bodytag
= str_ireplace("%body%", "black", "<body text=%BODY%>");
?>


Notes

Note:

This function is binary safe.

Code Examples / Notes » str_ireplace

hans111

Yet another one str_ireplace ireplacement, this one will take arrays as $search and $replace
<?php
function make_pattern(&$pat, $key) {
  $pat = '/'.preg_quote($pat, '/').'/i';
}
if(!function_exists('str_ireplace')){
function str_ireplace($search, $replace, $subject){
if(is_array($search)){
array_walk($search, 'make_pattern');
}
else{
$search = '/'.preg_quote($search, '/').'/i';
}
return preg_replace($search, $replace, $subject);
}
}
?>


aidan

This functionality is now implemented in the PEAR package PHP_Compat.
More information about using this function without upgrading your version of PHP can be found on the below link:
http://pear.php.net/package/PHP_Compat


lev

There appears to be a "bug" with this function in at least version 5.2.1.
If you attempt to use the function while replacing special characters, such as new lines, it will cause your entire scripts to fail. It resulted in my browser (Firefox 2) trying to download the file instead of parsing it.
Here is example code which will cause problems:
<?php
$sometext = "this is some text\n";
$sometext .= "that has a new line in it";
echo break_lines($sometext);
function break_lines ($text)
{
return str_ireplace("\n", "<br/>", $text);
}
?>
However, if you simply get rid of the "i" and use the normal str_replace, the page will load just fine.
I have also tried the same code on 5.2.3 and this version does NOT have the same problem, so it does appear to be an issue that was resolved with the newest builds of php.
I don't think it was a result of my personal environment, but I am running Debian-Sarge, Apache 2 and PHP 5.2.1/5.2.3.


rené johnson

str_ireplace for php below version 5. :)
if(!function_exists('str_ireplace')) {
function str_ireplace($search,$replace,$subject) {
$search = preg_quote($search, "/");
return preg_replace("/".$search."/i", $replace, $subject); } }


hfuecks

Note that character case is being defined by your server's locale setting, which effects strings containing non-ASCII characters.
See strtolower() - http://www.php.net/strtolower and comments - internally str_ireplace converts $search and $replace to lowercase to find matches.


aidan

If you want to do string highlighting, for example highlighting search terms, try str_highlight().
http://aidanlister.com/repos/v/function.str_highlight.php


daevid

here's a neat little function I whipped up to do HTML color coding of SQL strings.
<?php
/**
* Output the HTML debugging string in color coded glory for a sql query
* This is very nice for being able to see many SQL queries
* @access public
* @return void. prints HTML color coded string of the input $query.
* @param string $query The SQL query to be executed.
* @author Daevid Vincent [daevid@LockdownNetworks.com]
*  @version 1.0
* @date     04/05/05
* @todo highlight SQL functions.
*/
function SQL_DEBUG( $query )
{
if( $query == '' ) return 0;
global $SQL_INT;
if( !isset($SQL_INT) ) $SQL_INT = 0;
//[dv] this has to come first or you will have goofy results later.
$query = preg_replace("/['\"]([^'\"]*)['\"]/i", "'<FONT COLOR='#FF6600'>$1</FONT>'", $query, -1);
$query = str_ireplace(
array (
'*',
'SELECT ',
'UPDATE ',
'DELETE ',
'INSERT ',
'INTO',
'VALUES',
'FROM',
'LEFT',
'JOIN',
'WHERE',
'LIMIT',
'ORDER BY',
'AND',
'OR ', //[dv] note the space. otherwise you match to 'COLOR' ;-)
'DESC',
'ASC',
'ON '
 ),
array (
"<FONT COLOR='#FF6600'><B>*</B></FONT>",
"<FONT COLOR='#00AA00'><B>SELECT</B> </FONT>",
"<FONT COLOR='#00AA00'><B>UPDATE</B> </FONT>",
"<FONT COLOR='#00AA00'><B>DELETE</B> </FONT>",
"<FONT COLOR='#00AA00'><B>INSERT</B> </FONT>",
"<FONT COLOR='#00AA00'><B>INTO</B></FONT>",
"<FONT COLOR='#00AA00'><B>VALUES</B></FONT>",
"<FONT COLOR='#00AA00'><B>FROM</B></FONT>",
"<FONT COLOR='#00CC00'><B>LEFT</B></FONT>",
"<FONT COLOR='#00CC00'><B>JOIN</B></FONT>",
"<FONT COLOR='#00AA00'><B>WHERE</B></FONT>",
"<FONT COLOR='#AA0000'><B>LIMIT</B></FONT>",
"<FONT COLOR='#00AA00'><B>ORDER BY</B></FONT>",
"<FONT COLOR='#0000AA'><B>AND</B></FONT>",
"<FONT COLOR='#0000AA'><B>OR</B> </FONT>",
"<FONT COLOR='#0000AA'><B>DESC</B></FONT>",
"<FONT COLOR='#0000AA'><B>ASC</B></FONT>",
"<FONT COLOR='#00DD00'><B>ON</B> </FONT>"
 ),
$query
 );
echo "<FONT COLOR='#0000FF'><B>SQL[".$SQL_INT."]:</B> ".$query."<FONT COLOR='#FF0000'>;</FONT></FONT>
\n";
$SQL_INT++;
} //SQL_DEBUG
?>


n00b

Dreamhost hasn't upgraded to PHP 5.0 -- I came up with my own alternative, trying to make it run as much like str_ireplace() as possible.  Adjust the switch-a-roo token if you think it may conflict with your needle in teh haystack.
## HOMEBREW str_ireplace() FOR PRE-PHP 5.0
if (!function_exists('str_ireplace')  {
 function str_ireplace($search,$replace,$subject) {
   $token = '^[[term^]';
   $haystack = strtolower($subject);
   $needle = strtolower($search);
   while (($pos=strpos($haystack,$needle))!==FALSE)  {
     $c++;
     $subject = substr_replace($subject,$token,$pos,strlen($search));
     $haystack = substr_replace($haystack,$token,$pos,strlen($search));
   }
   while (($pos=strpos($subject,$token))!==FALSE)  {
     $subject = substr_replace($subject,$replace,$pos,strlen($token));
   }
   return $subject;
 }
}


hans111

<?php
if(!function_exists('str_ireplace')) {
function str_ireplace($search, $replacement, $string){
$delimiters = array(1,2,3,4,5,6,7,8,14,15,16,17,18,19,20,21,22,23,24,25,
26,27,28,29,30,31,33,247,215,191,190,189,188,187,186,
185,184,183,182,180,177,176,175,174,173,172,171,169,
168,167,166,165,164,163,162,161,157,155,153,152,151,
150,149,148,147,146,145,144,143,141,139,137,136,135,
134,133,132,130,129,128,127,126,125,124,123,96,95,94,
63,62,61,60,59,58,47,46,45,44,38,37,36,35,34);
foreach ($delimiters as $d) {
if (strpos($string, chr($d))===false){
$delimiter = chr($d);
break;
}
}
if (!empty($delimiter)) {
return preg_replace($delimiter.quotemeta($search).$delimiter.'i', $replacement, $string);
}
else {  
trigger_error('Homemade str_ireplace could not find a proper delimiter.', E_USER_ERROR);
}
}
}
?>


Change Language


Follow Navioo On Twitter
addcslashes
addslashes
bin2hex
chop
chr
chunk_split
convert_cyr_string
convert_uudecode
convert_uuencode
count_chars
crc32
crypt
echo
explode
fprintf
get_html_translation_table
hebrev
hebrevc
html_entity_decode
htmlentities
htmlspecialchars_decode
htmlspecialchars
implode
join
levenshtein
localeconv
ltrim
md5_file
md5
metaphone
money_format
nl_langinfo
nl2br
number_format
ord
parse_str
print
printf
quoted_printable_decode
quotemeta
rtrim
setlocale
sha1_file
sha1
similar_text
soundex
sprintf
sscanf
str_getcsv
str_ireplace
str_pad
str_repeat
str_replace
str_rot13
str_shuffle
str_split
str_word_count
strcasecmp
strchr
strcmp
strcoll
strcspn
strip_tags
stripcslashes
stripos
stripslashes
stristr
strlen
strnatcasecmp
strnatcmp
strncasecmp
strncmp
strpbrk
strpos
strrchr
strrev
strripos
strrpos
strspn
strstr
strtok
strtolower
strtoupper
strtr
substr_compare
substr_count
substr_replace
substr
trim
ucfirst
ucwords
vfprintf
vprintf
vsprintf
wordwrap
eXTReMe Tracker