Php preg_更换不在for循环中工作
我想使用短代码包含文件,但问题是preg_replace在for循环中无法正常工作。我的代码如下:Php preg_更换不在for循环中工作,php,regex,for-loop,Php,Regex,For Loop,我想使用短代码包含文件,但问题是preg_replace在for循环中无法正常工作。我的代码如下: <?php $output = "[@include('file1')] [@include('file2')]"; if(preg_match_all("/\[\@include\(\'(.*?)\'\)\]/", $output, $match)){ for($i=0;$i<count($match);$i++){ $output = preg_re
<?php
$output = "[@include('file1')] [@include('file2')]";
if(preg_match_all("/\[\@include\(\'(.*?)\'\)\]/", $output, $match)){
for($i=0;$i<count($match);$i++){
$output = preg_replace("/\[\@include\(\'(.*?)\'\)\]/", $match[1][$i], $output);
}
}
echo $output;
您要查找的内容已经在正则表达式的输出数组中(即在$match
数组中),您只需要使用
获取它
是否只获取“file1 file2”
?是的,但是file1 file2可能会更改。您正在逃避额外的内容。您可以只使用/\[@include\('.*?'\)\]/
。@
不是特别的,'
也不是特别的。如果在模式周围使用单引号,则需要转义单引号。
<?php
$output = "[@include('file1')] [@include('file2')]";
preg_match_all("/\[\@include\(\'(.*?)\'\)\]/", $output, $match);
echo $out = implode(' ', $match[1]);
?>