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);
字母l
和p
将被a
和代替e
。
如果它们的大小完全相同,为什么不能使用完整的字母数组呢?
str_replace
与数组只是顺序执行所有替换。使用strtr
,而不是做一次全部:
$new_message = strtr($message, 'lmnopq...', 'abcdef...');
由于str_replace()从左到右替换,因此在进行多次替换时它可能替换以前插入的值。
//输出F,因为A被B取代,然后B被C取代,依此类推... //由于从左到右的替换,最后用F替换了E。 $ search = array('A','B','C','D','E'); $ replace = array('B','C','D','E','F'); $ subject ='A'; 回声str_replace($ search,$ replace,$ subject);
比str_replace
以下方法更容易和更好:
<?php
$arr = array(
"http://" => "http://www.",
"w" => "W",
"d" => "D");
$word = "http://desiweb.ir";
echo strtr($word,$arr);
?>
strtr
PHP文档 在这里
除了标记为正确的答案之外,如果您必须替换单词而不是字符,则可以使用以下代码来实现:
$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
如果文本是简单标记且具有现有锚,则首先暂存现有的锚标记,换出网址,然后替换暂存的标记。
$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;
本文地址:http://php.askforanswer.com/str_replaceyushuzu.html
文章标签:arrays , php , str-replace
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
文章标签:arrays , php , str-replace
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
评论已关闭!