0

假设我有一个这样的字符串:

$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);

没有一个工作...

4

6 回答 6

2
$str = str_replace(array('}', '{'), '', $str);

str_replace接受数组作为它的第一个和第二个参数

于 2012-05-11T04:56:01.173 回答
2

你可以给一个数组来替换 str 看看

$search = array("}", "{");
$text= str_replace($search, "", $text);

在这里阅读:str-replace

于 2012-05-11T04:57:49.987 回答
1
str_replace (array('}', '{'), '', $str);
于 2012-05-11T04:56:03.723 回答
1
$str = str_replace(array('{', '}'), '', $str);
于 2012-05-11T04:57:40.503 回答
0

您想要做的是使用 preg_replace 函数,而不是使用正则表达式一次替换多个项目。您想要做的可以通过以下方式完成:

$str = preg_replace('/({|})/', '', $str);
于 2012-05-11T04:55:49.393 回答
0

$search = 数组("}", "{"); $text=str_replace($search, "", $text);

你可以阅读更多:- 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;
?>
于 2012-05-11T05:06:08.893 回答