Sorry if this question has already been answered elsewhere. I looked through stack overflow and couldn’t find exactly what I was looking for.
I need to know how to scan multiple php files in a single directory (test/ for example), and extract text between specific “tagged” areas on each php file.
Example of “tagged” areas:
<?
/*
{('test1')}
*/
?>
<div>text here</div>
<?
/*
{('test2')}
*/
?>
And the code would display test1, test2, etc. and ignore anything else. I tried looking into fopen(), file_get_contents and preg_match_all but each time they only find the first occurrence and not every occurrence of the “tagged” areas. Any help would be great!
EDIT – WHAT I CURRENTLY HAVE:
foreach (glob("templates/*.php") as $fn) {
$file = file_get_contents($fn);
preg_match_all("#\{\('(\w+)'\)}#", $file, $matches);
$variable = join('', $matches[1]);
echo $variable.'<br />';
How do I add array_chunk to this so that each iteration of test is echo’d as it’s own variable instead of grouped into an array. I tried this:
$variable = array_chunk($matches[1],1);
with no success, it just prints “Array”. any help would be great things. I will post in a new question if I don’t get a response.
This is how you would escape the regex:
Eugen has shown how to match the PHP/PI
<?tags and/*comment sections as well. You may just need\s*in between those.