假设我有一个这样的字符串:
$ str =“{aaa,aa,a,aaaaaaaa,aaaaaaa,aaaaaaa}”;
我想删除{
& }
只使用str_replace
一次 ..可能吗?
我试过
$ str = str_replace('}','{','',$ str);
$ str = str_replace('}'&'{','',$ str);
$ str = str_replace('}'||'{','',$ str);
$ str = str_replace('}''{','',$ str);
并且没有效果......
答案 0 :(得分:2)
$str = str_replace(array('}', '{'), '', $str);
str_replace
接受数组作为其第一个和第二个参数
答案 1 :(得分:1)
str_replace (array('}', '{'), '', $str);
答案 2 :(得分:1)
$str = str_replace(array('{', '}'), '', $str);
答案 3 :(得分:1)
你可以给str替换一个数组看看
$search = array("}", "{");
$text= str_replace($search, "", $text);
在此处阅读:str-replace
答案 4 :(得分:0)
您要做的是使用preg_replace函数,它将使用正则表达式一次替换多个项目。您想要做的事情可以通过以下方式完成:
$str = preg_replace('/({|})/', '', $str);
答案 5 :(得分:0)
$ search = array(“}”,“{”); $ text = str_replace($ search,“”,$ text);
yuo可以阅读更多内容: - http://php.net/manual/en/function.str-replace.php
例如
<?php
// Order of replacement
$str = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
$order = array("\r\n", "\n", "\r");
$replace = '<br />';
// Processes \r\n's first so they aren't converted twice.
$newstr = str_replace($order, $replace, $str);
// 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;
?>