繁体   English   中英

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

[英]Grab text between specific tags in PHP file

对不起,如果这个问题已在别处得到解答。 我查看了堆栈溢出,无法找到我正在寻找的确切内容。

我需要知道如何扫描单个目录中的多个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);

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

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

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

    $file = file_get_contents($fn);

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

    print_r($matches);

}

Eugen已经展示了如何匹配PHP / PI <? 标签和/*评论部分。 你可能只需要在这些之间使用\\s*

$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";
}

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

http://simplehtmldom.sourceforge.net/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM