我需要像这样获取评论 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);
但他们都不适合我,不知道为什么
你的问题中的这个表达:
preg_match_all("/(<funcion>)(.*?)(<\/funcion>)/s", $file, $matches);
print_r($matches);
这会起作用,但前提是$file
是包含 XML 的字符串;如果是文件名,则必须先获取内容:
preg_match_all("/(<funcion>)(.*?)(<\/funcion>)/s", file_get_contents($file), $matches);
另外,请记住 PCRE 有回溯限制 http://www.php.net/manual/en/pcre.configuration.php#ini.pcre.backtrack-limit当你使用非贪婪模式时。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)