46

str_replace在使用数组时遇到了 PHP 函数的一些问题。

我有这个消息:

$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);

字母lp将被替换为aand e

如果它们的大小完全相同,为什么它不能与完整的字母数组一起使用?

4

5 回答 5

57

因为 str_replace() 从左到右替换,所以在进行多次替换时,它可能会替换先前插入的值。

    // 输出 F,因为 A 被 B 替换,然后 B 被 C 替换,依此类推...
    // 最后 E 被 F 替换,因为从左到右替换。
    $search = array('A', 'B', 'C', 'D', 'E');
    $replace = array('B', 'C', 'D', 'E', 'F');
    $主题 = 'A';
    echo str_replace($search, $replace, $subject);
于 2012-12-05T03:38:26.667 回答
42

str_replacewith 数组只是按顺序执行所有替换。strtr改为一次完成所有操作:

$new_message = strtr($message, 'lmnopq...', 'abcdef...');
于 2012-12-05T03:30:19.180 回答
23

简单且优于str_replace

<?php
$arr = array(
    "http://" => "http://www.",
    "w" => "W",
    "d" => "D");

    $word = "http://desiweb.ir";
    echo strtr($word,$arr);
?>

strtrPHP文档 在这里

于 2016-02-06T14:57:16.423 回答
3

除了标记为正确的答案之外,如果您必须替换单词而不是字符,您可以使用以下代码来完成:

$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

于 2017-01-06T16:58:10.643 回答
0

如果文本是一个简单的标记并且具有现有的锚点,请先暂存现有的锚点标签,换掉 url,然后替换暂存的标记。

$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;
于 2019-09-27T15:34:18.563 回答