php Str_replace method, replacing string
The format is as follows:
Mixed Str_replace (mixed $search, mixed $replace, mixed $subject [, int & $count])
The target value of search lookup, which is needle. An array can specify multiple targets.
replacement value for replace search. An array can be used to specify multiple substitutions.
subject Executes the replaced array or string. That's haystack. If subject is an array, the replace operation will traverse the entire subject, and the return value will be an array.
If count is specified, its value is set to the number of times the substitution occurs. That is, how many substitutions have occurred altogether.
Description
If search and replace are arrays, then Str_replace () replaces the mappings for both subject.
If the value of replace is less than the number of search, redundant substitutions are made using an empty string.
If search is an array and replace is a string, then the substitution of each element in search always uses that string.
The Str_replace substitution method is case-sensitive.
Example:
<?php$str = ' abcdefgh '; Echo str_replace (' abc ', ' 123 ', $str); 123defgh$str = ' 123456 '; $search = Array (1, 2, 3, 4, 5, 6); $replace = Array (' A ', ' B ', ' C ', ' d ', ' f ', ' G '); Echo Str_repla CE ($search, $replace, $STR); Abcdefg$arr = Array (' abc ', ' BAC ', ' CBA '); $result = Str_replace (' B ', ' B ', $arr, $count);p Rint_r ($result); Array ([0] = aBc [1] = BAC [2] = = CBa) echo $count; 3 Total replaced 3 times?>
It is convenient to replace strings with str_replace, but all values that match search will be replaced with the value of replace. If you want to replace the specified number of times, this method will not be implemented.
For example:user_order_list replaced by user/order_list
<?php$str = ' user_order_list '; Echo str_replace (' _ ', '/', $str); User/order/list?>
The method that replaces the specified number of times can be implemented using the regular preg_replace method.
<?php/** * A specified number of times to execute a string replace * @param Mixed $search Find the target value * @param Mixed $replace Replacement value * @param Mixed $s Ubject executes the replacement string/array * @param Int $limit number of substitutions allowed, default is-1, unlimited * @return Mixed */function Str_replace_ Limit ($search, $replace, $subject, $limit =-1) { if (Is_array ($search)) { foreach ($search as $k + = $v) { $search [$k] = ". Preg_quote ($search [$k],"). '`'; } } else{ $search = ". Preg_quote ($search,"). '`'; } Return Preg_replace ($search, $replace, $subject, $limit);}? >
Example:
<?php$str = ' user_order_list '; Echo str_replace_limit (' _ ', '/', $STR, 1); User/order_list$arr = Array (' ABBC ', ' Bbac ', ' Cbba '); $result = Str_replace_limit (' B ', ' B ', $arr, 1);p Rint_r ($result); Array ([0] = ABBC [1] = BBAC [2] = Cbba)?>
This article explains the PHP str_replace to replace the specified number of times method, more relevant content please focus on PHP Chinese web.
Related recommendations:
About Header,headers_sent,headers_list,header_remove Usage Instructions
Query MySQL return field by PDO the integer type becomes a string-based workaround
Get country, province, city, and peripheral data by using PHP based on geographic coordinates