是否可以使用自定义模式拆分文件内容?

Ahm*_*uad 2 php regex parsing file-get-contents

是否可以将文件内容拆分为具有特定模式的部分?

这就是我想要实现的目标:

  • 使用file_get_contents读取文件
  • 只读相似注释区域之间的内容.

我不确定这有多复杂,但基本上如果我正在解析一个大的html文件,并且只希望向浏览器显示特定的小部件(模式是注释边界),如下所示:

样品:

<html>
<head>
   <title>test</title>
</head>
<body>
 this content should not be parsed.. ignored
 <!-- widget -->
 this is the widget. i want to parse this content only from the file
 <!-- widget -->
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

是否有可能使用PHP和正则表达式或任何东西来解析边界之间的内容?

我道歉但我试图尽可能多地解释我想要实现的目标.希望有人帮助我.

Cha*_*rch 6

这当然是可能的,但它并不需要用正则表达式完成.我可能只是这样做:

$file = file_get_contents('http://example.com/');
$widgets = explode('<!-- widget -->', $file);
Run Code Online (Sandbox Code Playgroud)

现在,$widget([1], [3], [5]等)的奇数元素包含这些边界之间的内容.