PHP正则表达式多行字符串的一部分

时间:2023-02-01 21:47:14

I want to have One part multiline in a regex

我希望在正则表达式中有一部分多行

<?php
 function bloggerLoop ( $matches ) { print_r ( $matches ) ; }

 $theme = preg_replace_callback(
         "#<b:loop values='data:(.*)' var='(.*)'>(.*?)</b:loop>#i",
         "bloggerLoop", 
         $theme 
         );
?>

This is what I have, but b:loop is more than once in the template and this regex skips all the loops and make one "big" loop, but i only need to have one "part".

这就是我所拥有的,但是b:循环在模板中不止一次,这个正则表达式会跳过所有循环并形成一个“大”循环,但我只需要一个“部分”。

What need is:

需要的是:

<b:loop values='data:x' var='x'>MULTILINE</b:loop>

The file what is needed to be parsed is: http://pastebin.com/a3g3G1uE

需要解析的文件是:http://pastebin.com/a3g3G1uE

I hope you can help me with the regex.

我希望你能帮助我使用正则表达式。

Tanks.

-Wesley

1 个解决方案

#1


1  

Regex is not the tool for this. HTML tag structure is too complex, for basic Regex, and you should try some HTML DOM library like PHP Simple HTML DOM Parser, to do HTML DOM work.

正则表达式不是这个的工具。 HTML标记结构太复杂了,对于基本的Regex,你应该尝试一些HTML DOM库,如PHP Simple HTML DOM Parser,来做HTML DOM工作。

$html = file_get_html('http://pastebin.com/raw.php?i=a3g3G1uE');
foreach($html->find('b:loop') as $element)
       echo $element->values. '<br>';

Look at my example for your specific case and read documentation.

请查看我的示例,了解您的具体案例并阅读文档。

#1


1  

Regex is not the tool for this. HTML tag structure is too complex, for basic Regex, and you should try some HTML DOM library like PHP Simple HTML DOM Parser, to do HTML DOM work.

正则表达式不是这个的工具。 HTML标记结构太复杂了,对于基本的Regex,你应该尝试一些HTML DOM库,如PHP Simple HTML DOM Parser,来做HTML DOM工作。

$html = file_get_html('http://pastebin.com/raw.php?i=a3g3G1uE');
foreach($html->find('b:loop') as $element)
       echo $element->values. '<br>';

Look at my example for your specific case and read documentation.

请查看我的示例,了解您的具体案例并阅读文档。