该函数返回一个字符串或者数组。该字符串或数组是将 subject
中全部的 search
都被 replace
替换(忽略大小写)之后的结果。如果没有一些特殊的替换规则,你应该使用该函数替换带有 i 修正符的 preg_replace() 函数。
search
Note:
search
元素的每次替换都是基于前一次替换结果的。
replace
subject
如果 subject
是一个数组,替换操作将遍历整个 subject
,并且也将返回一个数组。
count
needles
的匹配和替换数量将在 count
中返回,它是以引用方式传递的。
如果 search
和 replace
为数组,那么 str_replace() 将对 subject
做二者的映射替换。如果 replace
的值的个数少于 search
的个数,多余的替换将使用空字符串来进行。如果 search
是一个数组而 replace
是一个字符串,那么 search
中每个元素的替换将始终使用这个字符串。
返回替换后的字符串或者数组。
版本 | 说明 |
---|---|
5.0.0 |
新增 count 参数。
|
Example #1 str_ireplace() 范例
<?php
$bodytag = str_ireplace("%body%", "black", "<body text=%BODY%>");
?>
Note: 此函数可安全用于二进制对象。
chud37.com (2012-01-11 12:52:36)
After searching through the forums and laboriously testing all the highlight functions (as I was too lazy to write my own, I wanted a quick solution), I found that none of them did what I wanted at all.
I just wanted a function that took a string and did not modify it in anyway, apart from just surrounding the matching words with some kind of highlighting HTML. At best all the ones of the forums here changed the text but also the case as well. The example given by sawdust *did* work except when given a single character when it continued to loop-replace the letter 'a' in the inserted 'background-color:'. So, here is a fool proof, no-nonsense replace method that should work always.
<?php
function highlight($haystack, $needle) {
if (strlen($haystack) < 1 || strlen($needle) < 1) {return $haystack;}
preg_match_all("/$needle+/i", $haystack, $match);
$exploded = preg_split("/$needle+/i",$haystack);
$replaced = "";
foreach($exploded as $e)
foreach($match as $m)
if($e!=$exploded[count($exploded)-1]) {$replaced .= $e . "<font style=\"background-color:yellow\">" . $m[0] . "</font>";} else {$replaced .= $e;}
return $replaced;
}
?>
Psudo - thepsudo at gmail dot com (2011-06-29 21:53:07)
For highlighting without the overhead of regex and without destroying capitalization, try this:
<?php
function highlight($needle, $haystack){
$ind = stripos($haystack, $needle);
$len = strlen($needle);
if($ind !== false){
return substr($haystack, 0, $ind) . "<b>" . substr($haystack, $ind, $len) . "</b>" .
highlight($needle, substr($haystack, $ind + $len));
} else return $haystack;
}
?>
This example uses HTML bold tags, but you can easily change the highlighting method.
holblin at holblin dot com (2011-03-09 13:11:14)
Warning with highlighting ...
I used :
<?php
$text = preg_replace('/('.$q.')/i','<span class=highlighting "">$1</span>' , $text);
?>
Because this line do not allow to highlight uppercase and lowercase correctly (transform uppercase to lowercase for exemple)
<?php
$text = str_ireplace( $q , '<span class=highlighting "">'.$q.'</span>', $text);
?>
But when $q contain some regex you have some problems ... for exemple :
<?php $q = '('; ?>
So you must use preg_replace to highlight correctly the text and you must create a function for escape bad regex caracters !
I think that a better function can be found but this works I guess :
<?php
function regex_escape( $q )
{
return preg_replace('/([\[\]\(\)\{\}\-\.\*\?\|\^\$])/', '\$1', $q);
}
?>
stepanic dot matija at gmail dot com (2010-10-21 20:57:50)
FIX-ed problem with highlighting second 'o' OR 'a', in this string
<?php
function highlight_string ($haystack, $needle, $highlight_class) {
// return $haystack if there is no highlight color or strings given, nothing to do.
$first_encode='XXXXXXXXXXXXXXX'; //ENCODE string
$second_encode='YYYYYYYYYYYYYYY';
preg_match_all("/$needle+/i", $haystack, $matches);
if (is_array($matches[0]) && count($matches[0]) >= 1) {
foreach ($matches[0] as $match) {
$haystack = str_replace($match, $first_encode.$match.$second_encode, $haystack);
}
}
$haystack=str_replace(array($first_encode,$second_encode),
array('<font class="'.$highlight_class.'" >','</font>'),$haystack);
return $haystack;
}
?>
triplepoint at gmail dot com (2010-09-23 10:06:57)
Regarding maintaining the case of the find/replace for search-highlighting purposes:
if the performance hit of a regular expression isn't a big problem, there's something like:
<?php
function highlight_matches($find_text, $text) {
return preg_replace("/($find_text)/i", '<span class="search_item">$1</span>', $text);
}
?>
ishutko at gmail dot com (2009-03-16 13:49:39)
For function work with cirilic
setlocale (LC_ALL, 'ru_RU');
sawdust (2008-12-04 07:28:46)
Here's a different approach to search result keyword highlighting that will match all keyword sub strings in a case insensitive manner and preserve case in the returned text. This solution first grabs all matches within $haystack in a case insensitive manner, and the secondly loops through each of those matched sub strings and applies a case sensitive replace in $haystack. This way each unique (in terms of case) instance of $needle is operated on individually allowing a case sensitive replace to be done in order to preserve the original case of each unique instance of $needle.
<?php
function highlightStr($haystack, $needle, $highlightColorValue) {
// return $haystack if there is no highlight color or strings given, nothing to do.
if (strlen($highlightColorValue) < 1 || strlen($haystack) < 1 || strlen($needle) < 1) {
return $haystack;
}
preg_match_all("/$needle+/i", $haystack, $matches);
if (is_array($matches[0]) && count($matches[0]) >= 1) {
foreach ($matches[0] as $match) {
$haystack = str_replace($match, '<span style="background-color:'.$highlightColorValue.';">'.$match.'</span>', $haystack);
}
}
return $haystack;
}
?>
Michael dot Bond at mail dot wvu dot edu (2008-11-14 06:44:21)
This function will highlight search terms (Key Words in Context).
The difference between this one and the ones below is that it will preserve the original case of the search term as well. So, if you search for "american" but in the original string it is "American" it will retain the capital "A" as well as the correct case for the rest of the string.
<?php
function kwic($str1,$str2) {
$kwicLen = strlen($str1);
$kwicArray = array();
$pos = 0;
$count = 0;
while($pos !== FALSE) {
$pos = stripos($str2,$str1,$pos);
if($pos !== FALSE) {
$kwicArray[$count]['kwic'] = substr($str2,$pos,$kwicLen);
$kwicArray[$count++]['pos'] = $pos;
$pos++;
}
}
for($I=count($kwicArray)-1;$I>=0;$I--) {
$kwic = '<span class="kwic">'.$kwicArray[$I]['kwic'].'</span>';
$str2 = substr_replace($str2,$kwic,$kwicArray[$I]['pos'],$kwicLen);
}
return($str2);
}
?>
info at daniel-marschall dot de (2008-10-02 10:32:11)
Example for str_ireplace(). It will print "RePlaCeMe" in red color, but after this, it would be written in lower case because of the string $search.
<?php
$search = 'replaceme';
$replace = '<font color="#FF0000">'.$search.'</font>';
$text = 'Please RePlaCeMe, OK?';
echo str_ireplace($search, $replace, $text);
?>
Example for ext_str_ireplace(). It will print "RePlaCeMe" in red color, and will not change the capitalization:
<?php
$search = 'replaceme';
$replace = '<font color="#FF0000">$1</font>';
$text = 'Please RePlaCeMe, OK?';
echo ext_str_ireplace($search, $replace, $text);
?>
hfuecks at nospam dot org (2005-07-04 02:07:59)
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.
daevid at daevid dot com (2005-04-05 13:14:52)
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><BR>\n";
$SQL_INT++;
} //SQL_DEBUG
?>
aidan at php dot net (2004-05-30 10:36:02)
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