Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何替换php中的特定字符,如括号_Php_Regex - Fatal编程技术网

如何替换php中的特定字符,如括号

如何替换php中的特定字符,如括号,php,regex,Php,Regex,在过去的三天里,我一直在尝试替换我自己的分隔符中的括号。差不多 ( ( [take] function ( [/take] ( 像 [take] function [ [/take] 不影响[take][/take]分隔符外的括号 我试过了 preg_replace('/[^\[take\]]([)[^\[\take\]]/', '[', $string); 如果间距一致,则: $result = str_replace('function ()', 'function []', '(

在过去的三天里,我一直在尝试替换我自己的分隔符中的括号。差不多

(  ( [take] function ( [/take] (

[take] function [ [/take]
不影响
[take][/take]
分隔符外的括号

我试过了

preg_replace('/[^\[take\]]([)[^\[\take\]]/', '[', $string);

如果间距一致,则:

$result = str_replace('function ()', 'function []', '( ( function () (');

您需要使用正则表达式:

$string = "( ( [take] function( [/take] (";
$result = preg_replace_callback(
    "/(\[take\])(.*?)(\[\/take\])/",
    function($m) {return $m[1] . str_replace("(", "[", $m[2]) . $m[3];},
    $string
);
echo $result;

允许您对匹配项执行回调函数。在这种情况下,我们使用匹配组(括号内)来隔离标记之间的文本,然后在回调中用括号替换括号。

[]替换
((function()(
)看起来像
[[function[][
。你能更清楚地解释一下你想要什么吗?我只想更改[take][/take]分隔符中任何一个方括号的开头。谢谢你的回答。谢谢你的回答。谢谢你的回答。我错误地键入了这个问题。它现在已被编辑。我感谢你的回答。