使用数组时,我遇到了PHP函数str_replace
的麻烦。
我有这样的信息:
$message = strtolower("L rzzo rwldd ty esp mtdsza'd szdepw ty esp opgtw'd dple");
我正试图像这样使用str_replace
:
$new_message = str_replace(
array('l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g','h','i','j','k'),
array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'),
$message);
结果应该是A good glass in the bishop's hostel in the devil's seat
,而是p voos vlpss xn twt qxswop's wosttl xn twt stvxl's stpt
。
但是,当我只尝试更换2个字母时,它会很好地替换它们:
$new_message = str_replace(array('l','p'), array('a','e'), $message);
字母l
和p
将替换为a
和e
。
如果它们的大小完全相同,为什么它不能使用完整的字母数组呢?
答案 0 :(得分:45)
因为str_replace()从左向右替换,所以在执行多次替换时,它可能会替换先前插入的值。
// Outputs F because A is replaced with B, then B is replaced with C, and so on... // Finally E is replaced with F, because of left to right replacements. $search = array('A', 'B', 'C', 'D', 'E'); $replace = array('B', 'C', 'D', 'E', 'F'); $subject = 'A'; echo str_replace($search, $replace, $subject);
答案 1 :(得分:35)
str_replace
只是按顺序执行所有替换。使用strtr
代替一次完成所有操作:
$new_message = strtr($message, 'lmnopq...', 'abcdef...');
答案 2 :(得分:15)
比str_replace
<?php
$arr = array(
"http://" => "http://www.",
"w" => "W",
"d" => "D");
$word = "http://desiweb.ir";
echo strtr($word,$arr);
?>
strtr
PHP文档 here
答案 3 :(得分:1)
除了标记为正确的答案外,如果你必须替换单词而不是字符,你可以用这段代码来完成:
$query = "INSERT INTO my_table VALUES (?, ?, ?, ?);";
$values = Array("apple", "oranges", "mangos", "papayas");
foreach (array_fill(0, count($values), '?') as $key => $wildcard) {
$query = substr_replace($query, '"'.$values[$key].'"', strpos($query, $wildcard), strlen($wildcard));
}
echo $query;
在这里演示:http://sandbox.onlinephpfunctions.com/code/56de88aef7eece3d199d57a863974b84a7224fd7
答案 4 :(得分:0)
如果文本是简单标记且具有现有锚,请首先暂存现有的锚标记,交换掉网址,然后替换暂存的标记。
$text = '
Lorem Ipsum is simply dummy text found by searching http://google.com/?q=lorem in your <a href=https://www.mozilla.org/en-US/firefox/>Firefox</a>,
<a href="https://www.apple.com/safari/">Safari</a>, or https://www.google.com/chrome/ browser.
Link replacements will first stage existing anchor tags, replace each with a marker, then swap out the remaining links.
Links should be properly encoded. If links are not separated from surrounding content like a trailing "." period then they it will be included in the link.
Links that are not encoded properly may create a problem, so best to use this when you know the text you are processing is not mixed HTML.
Example: http://google.com/i,m,complicate--d/index.html
Example: https://www.google.com/chrome/?123&t=123
Example: http://google.com/?q='. urlencode('<a href="http://google.com">http://google.com</a>') .'
';
// Replace existing links with a marker
$linkStore = array();
$text = preg_replace_callback('/(<a.*?a>)/', function($match) use (&$linkStore){ $key = '__linkStore'.count($linkStore).'__'; $linkStore[$key] = $match[0]; return $key; }, $text);
// Replace remaining URLs with an anchor tag
$text = preg_replace_callback("/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/", function($match) use (&$linkStore){ return '<a href="'. $match[0] .'">'. $match[0] .'</a>'; }, $text);
// Replace link markers with original
$text = str_replace(array_keys($linkStore), array_values($linkStore), $text);
echo '<pre>'.$text;