在PHP文件中的特定标记之间抓取文本

时间:2012-01-26 17:37:10

标签: php regex tags file-get-contents preg-match-all

很抱歉,如果这个问题已在其他地方得到解答。我查看了堆栈溢出,无法找到我正在寻找的确切内容。

我需要知道如何扫描单个目录中的多个php文件(例如test /),并在每个php文件的特定“标记”区域之间提取文本。

“已标记”区域的示例:

<?
/*
{('test1')}
*/
?>

<div>text here</div>

<?
/*
{('test2')}
*/
?>

代码将显示test1,test2等,并忽略其他任何内容。我试着查看fopen(),file_get_contents和preg_match_all,但每次只找到第一次出现而不是每次出现“标记”区域。任何帮助都会很棒!

编辑 - 我当前有什么:

foreach (glob("templates/*.php") as $fn) {

$file = file_get_contents($fn);

preg_match_all("#\{\('(\w+)'\)}#", $file, $matches);   

$variable = join('', $matches[1]);

echo $variable.'<br />';

如何将array_chunk添加到此处,以便每次迭代测试都是echo,因为它是自己的变量而不是分组到数组中。我试过这个:

$variable = array_chunk($matches[1],1);

没有成功,它只是打印“数组”。任何帮助都会很棒。如果我没有得到答复,我会发一个新问题。

3 个答案:

答案 0 :(得分:2)

这就是你如何逃避正则表达式:

foreach (glob("template/*.php") as $fn) {

    $file = file_get_contents($fn);

    preg_match_all("#\{\('(\w+)'\)}#", $file, $matches);   

    print_r($matches);

}

Eugen已经展示了如何匹配PHP / PI <?标签和/*评论部分。您可能只需要\s*

答案 1 :(得分:0)

$filepattern='test/*.php';
$tagpattern='/\<\?\n\/\*\n\{\(\'([^\']+)\'\)\}\n\*\/\n\?\>/';

$files=glob($filepattern);
foreach ($files as $file) {
  $content=file_get_contents($file);
  $count=preg_match_all($tagpattern,$content,$matches);
  if ($count<1) continue;

  //Whatever you want to do with the matches!
  foreach ($matches[1] as $match) echo "$file: $match\n";
}

答案 2 :(得分:-1)

正则表达式不合适且速度慢。尝试php dom或这个漂亮的库

http://simplehtmldom.sourceforge.net/

相关问题