使用现有的 str_replace 用单词和 URL 替换 & 号 [英] Replacing ampersand with the word and in URL's using existing str_replace
本文介绍了使用现有的 str_replace 用单词和 URL 替换 & 号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要用 URL 中的单词 and
替换与号 (&
),并且我已经使用 php str_replace
如下图:-
I'm needing to replace ampersand (&
) with the word and
in URL's and am already replacing spaces with hyphens using php str_replace
like below:-
<?php echo strtolower(str_replace(' ', '-', $value)) ?>
我是否可以修改它以通过使用数组来添加&符号的替换?
Am I able to modify this to add the replacement of ampersands as well by using an array perhaps?
推荐答案
要在一个语句中替换两个字符串,请执行以下操作;
To replace both strings in one statement, do the following;
<?php
$find = array(" ", "&");
$replace = array("-", "and");
$string = "Hello I am a man & I have a dog";
echo str_replace($find, $replace, $string); //Output: Hello-I-am-a-man-and-I-have-a-dog
一种更优雅的方式是拥有一个关联数组.(http://codepad.org/OgogWK5l)
A more elegant way would be to have one associative array. (http://codepad.org/OgogWK5l)
<?php
$findAndReplace = array(" " => "-", "&" => "and");
$string = "Hello I am a man & I have a dog";
echo str_replace(array_keys($findAndReplace), array_values($findAndReplace), $string);
这篇关于使用现有的 str_replace 用单词和 URL 替换 & 号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文