Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.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_Preg Match - Fatal编程技术网

Php 预匹配对于存在的每个封闭文本,重复一个函数

Php 预匹配对于存在的每个封闭文本,重复一个函数,php,regex,preg-match,Php,Regex,Preg Match,在字符串中,我有如下内容: [select|blog] Some text some text some text [/select] more text more text more text [select|information] Some text some text some text [/select] Changed text1 Changed text1 Changed text1 more text more text more text Change

在字符串中,我有如下内容:

[select|blog]
    Some text some text some text
[/select]

more text more text more text

[select|information]
    Some text some text some text
[/select]
Changed text1 Changed text1 Changed text1

more text more text more text

Changed text2 Changed text2 Changed text2
现在使用preg_match,我希望函数:

  • 获取[select]标记之间的文本
  • 改变它
  • 将其插入到字符串中
  • 删除选择标记
所以最终会变成这样:

[select|blog]
    Some text some text some text
[/select]

more text more text more text

[select|information]
    Some text some text some text
[/select]
Changed text1 Changed text1 Changed text1

more text more text more text

Changed text2 Changed text2 Changed text2

希望你们能帮我。谢谢!:)

这个正则表达式将帮助您开始

\[.*?](.*?)\[.*?]
将选项置于允许点的位置。配新线

括号内的文本在第1组中

我来自.NET世界,所以我会用一个函数来解决这个问题,我可以让它在每次比赛后执行。 此函数的输入为匹配。在这里,如果您需要访问组1并重写或放置新内容,您可以访问组1


我刚刚在评论中读到,php中的等价物是
preg\u replace\u callback()

,你会发现你的答案质量更好,如果你能够证明你有,人们会更愿意帮助你。可能是开始的地方。+1,另外一个提示:
/s
PCRE修饰符将有助于herebuckley,为什么我在开始使用正则表达式时会出现这个错误:分隔符不能是字母数字或反斜杠…我发现括号也必须环绕整个模式:(\{select\\\.*?}(.*?\{/select.*?})谢谢;)