我使用str_replace替换如下一个字符:
str_replace("--","/",$value['judul'])。
但我想替换两个角色,像这样:
str_replace("--","/",$value['judul'])和
str_replace("-+-",":",$value['judul'])
而不做两个str_replace。我能用一个str-replace吗?
发布于 2017-09-02 01:03:14
您可以使用strtr()和关联数组来完成以下操作:
<?php
$text = "Text about -- and -+- !";
$replacements = [
"--" => "/",
"-+-" => ":",
];
echo strtr($text, $replacements); // Text about / and : !要添加更多的替换,只需继续向$replacements数组添加更多的元素。索引是要查找的字符串,值是替换。
https://stackoverflow.com/questions/46009553
复制相似问题