php - preg_replace not working in for loop -
i want include files using shortcode, problem preg_replace isn't working in loop.my code below:
<?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; the above code prints "file1 file1", should print "file1 file2" both file name not printing both files name.please tell i'm wrong.
what you're looking in output array of regular expression (i.e. in $match array), need using implode()!
<?php $output = "[@include('file1')] [@include('file2')]"; preg_match_all("/\[\@include\(\'(.*?)\'\)\]/", $output, $match); echo $out = implode(' ', $match[1]); ?>
Comments
Post a Comment