【发布时间】:2013-02-15 12:11:40
【问题描述】:
我需要在这样的注释 php 文件中获取一些标签内的值
php code
/* this is a comment
!-
<titulo>titulo3</titulo>
<funcion>
<descripcion>esta es la descripcion de la funcion 6</descripcion>
</funcion>
<funcion>
<descripcion>esta es la descripcion de la funcion 7</descripcion>
</funcion>
<otros>
<descripcion>comentario de otros 2a hoja</descripcion>
</otros>
-!
*/
some php code
所以你可以看到文件有换行符和重复的标签,如<funcion></funcion>,我需要获取每一个标签,所以我尝试了这样的事情:
preg_match_all("/(<funcion>)(.*)(<\/funcion>)/s",$file,$matches);
此示例适用于换行符,但它很贪心,所以我一直在搜索并看到这两种解决方案:
preg_match_all("/(<funcion>)(.*?)(<\/funcion>)/s",$file,$matches);
preg_match_all("/(<funcion>)(.*)(<\/funcion>)/sU",$file,$matches);
但它们都不适合我,不知道为什么
【问题讨论】:
-
只需解析 XML。
-
@Blender 它不是一个真正的 xml,它应该在一个 php 文件的注释中。我将对其进行编辑,以便更清楚
-
我写了一个答案,但我刚刚意识到您发布的第一个示例(实际上是第二个)实际上在这里完美运行。
-
@Raphael_ 它对我不起作用,哈哈
-
在this codepad 中运行良好。
标签: php regex regex-greedy non-greedy