[英]Replace all substring instances with a variable string
如果你有字符串
'Old string Old more string Old some more string'
你想得到
'New1 string New2 more string New3 some more string'
你會怎么做?
換句話說,您需要用變量字符串“New”.$i 替換“Old”的所有實例。 如何做呢?
不需要正則表達式的迭代解決方案:
$str = 'Old string Old more string Old some more string';
$old = 'Old';
$new = 'New';
$i = 1;
$tmpOldStrLength = strlen($old);
while (($offset = strpos($str, $old, $offset)) !== false) {
$str = substr_replace($str, $new . ($i++), $offset, $tmpOldStrLength);
}
strpos()
中的$offset
只是一點點微優化。 我不知道,它是否值得(事實上我什至不知道,它是否改變了什么),但想法是我們不需要在已經處理過的 substring 中搜索$old
。
看演示
Old string Old more string Old some more string
New1 string New2 more string New3 some more string
$count = 0;
$str = preg_replace_callback(
'~Old~',
create_function('$matches', 'return "New".$count++;'),
$str
);
來自str_replace
的 PHP 手冊:
用替換字符串替換所有出現的搜索字符串
mixed str_replace ( mixed $search, mixed $replace, mixed $subject [, int &$count ] )
搜索
正在搜索的值,也稱為needle 。 一個數組可以用來指定多個針。
代替
替換找到的搜索值的替換值。 數組可用於指定多個替換。
學科
正在搜索和替換的字符串或數組,也稱為haystack 。
如果subject是一個數組,則對subject的每個條目執行搜索和替換,返回值也是一個數組。
數數
如果通過,這將設置為執行的替換次數。
采用:
$str = 'Old string Old more string Old some more string';
$i = 1;
while (preg_match('/Old/', $str)) {
$str = preg_replace('/Old/', 'New'.$i++, $str, 1);
}
echo $str,"\n";
Output:
New1 string New2 more string New3 some more string
我有一些類似KingCrunch 的解決方案,但正如他已經回答的那樣,我想知道一個帶有替換回調的str_replace
變體,並想出了這個(演示):
$subject = array('OldOldOld', 'Old string Old more string Old some more string');
$search = array('Old', 'string');
$replace = array(
function($found, $count) {return 'New'.$count;},
function($found, $count) {static $c=0; return 'String'.(++$c);}
);
$replace = array();
print_r(str_ureplace($search, $replace, $subject));
/**
* str_ureplace
*
* str_replace like function with callback
*
* @param string|array search
* @param callback|array $replace
* @param string|array $subject
* @param int $replace_count
* @return string|array subject with replaces, FALSE on error.
*/
function str_ureplace($search, $replace, $subject, &$replace_count = null) {
$replace_count = 0;
// Validate input
$search = array_values((array) $search);
$searchCount = count($search);
if (!$searchCount) {
return $subject;
}
foreach($search as &$v) {
$v = (string) $v;
}
unset($v);
$replaceSingle = is_callable($replace);
$replace = $replaceSingle ? array($replace) : array_values((array) $replace);
foreach($replace as $index=>$callback) {
if (!is_callable($callback)) {
throw new Exception(sprintf('Unable to use %s (#%d) as a callback', gettype($callback), $index));
}
}
// Search and replace
$subjectIsString = is_string($subject);
$subject = (array) $subject;
foreach($subject as &$haystack) {
if (!is_string($haystack)) continue;
foreach($search as $key => $needle) {
if (!$len = strlen($needle))
continue;
$replaceSingle && $key = 0;
$pos = 0;
while(false !== $pos = strpos($haystack, $needle, $pos)) {
$replaceWith = isset($replace[$key]) ? call_user_func($replace[$key], $needle, ++$replace_count) : '';
$haystack = substr_replace($haystack, $replaceWith, $pos, $len);
}
}
}
unset($haystack);
return $subjectIsString ? reset($subject) : $subject;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.