Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.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 preg_更换不在for循环中工作_Php_Regex_For Loop - Fatal编程技术网

Php preg_更换不在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

我想使用短代码包含文件,但问题是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_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]);
?>