Php str_replace()用于多值替换
是否可以使用Php str_replace()用于多值替换,php,Php,是否可以使用str_replace在一行中进行多值替换。例如,我想用'-'替换',用'?接受数组作为参数 例如: $subject = 'milk is white and contains sugar'; str_replace(array('sugar', 'milk'), array('sweet', 'white'), $subject); $subject = array('milk contains sugar', 'sugar is white', 'sweet as sugar
str_replace
在一行中进行多值替换。例如,我想用'-'
替换'
,用'
?接受数组作为参数
例如:
$subject = 'milk is white and contains sugar';
str_replace(array('sugar', 'milk'), array('sweet', 'white'), $subject);
$subject = array('milk contains sugar', 'sugar is white', 'sweet as sugar');
str_replace(array('sugar', 'milk'), array('sweet', 'white'), $subject);
事实上,第三个参数也可以是数组,因此您可以通过一个str\u replace()
调用在多个值中进行多个替换
例如:
$subject = 'milk is white and contains sugar';
str_replace(array('sugar', 'milk'), array('sweet', 'white'), $subject);
$subject = array('milk contains sugar', 'sugar is white', 'sweet as sugar');
str_replace(array('sugar', 'milk'), array('sweet', 'white'), $subject);
正如其他人所指出的,手册中明确说明了这一点:
搜索
正在搜索的值,也称为指针。一个数组可用于指定多个针
更换
替换找到的搜索值的替换值。一个数组可用于指定多个替换
主题
正在搜索和替换的字符串或数组,也称为haystack
注意数组的顺序,因为它对应于顺序。 就像下面的例子一样,A替换为B,B替换为C,依此类推。。好了。
// 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);
// Outputs: apearpearle pear
// For the same reason mentioned above
$letters = array('a', 'p');
$fruit = array('apple', 'pear');
$text = 'a p';
$output = str_replace($letters, $fruit, $text);
echo $output;
?>
来源:是的,借助str_replace函数,我们可以在没有数组的情况下在一行中进行多值替换。这是我的代码
echo str_replace(" ","-",str_replace("&","","I like Tea&Coffee"));
我想用“$”替换“a”,用“!”替换“b”,所以我需要这样写:
$str = ['a','b'];
$rplc =['$','!'];
echo str_replace($str,$rplc,$name);
输出:
$!cd
我就是这样做的,将“to”替换为“to”,这样就不会打断SQL查询和“with”,因为人们总是在电子邮件末尾添加空格:
str_replace([' ','-','&'],'',$text);
$check = str_replace("'","''",$_POST['1']);
$checkbox1 = str_replace(" ","",$check);
你可以这样做:
$check = str_replace("&","",$_POST['1']);
$checkbox1 = str_replace(" ","-",$check);
这回答了你的问题吗?这个答案显然没有经过测试,而且充满了杀脚本的拼写错误。这个答案是如何获得UV的?为什么这个答案不在顶部?我几乎写了一篇阅读上述答案的
foreach
声明。您可能选择了按日期而不是按评级对答案进行排序。通常,这会给出最新和最相关的答案,但也会降低高评价或被接受的答案。这个答案就是猫的睡衣,这就是我要说的