简体   繁体   English

php preg_match 模式用于解析标签

[英]php preg_match pattern for parsing tags

I want to know a good preg_match pattern in php for extracting data between tags.我想知道 php 中用于提取标签之间数据的良好 preg_match 模式。

For example:例如:

 <page>
    <username>someone</username>
    <id>3020778</id>
    <text xml:space="preserve"> The quick brown fox. </text>
 </page>

This will give me the string "The quick brown fox".这会给我字符串“The quick brown fox”。

I have tried using我试过使用

preg_match('/<text(.*)?>(.*)?<\/text>/', $content, $match);

But it seems doesn't work on some other cases.但它似乎不适用于其他一些情况。

Does anyone have a better solution or pattern?有没有人有更好的解决方案或模式?

And does using simpleXML make it more faster than preg_match?使用 simpleXML 是否比 preg_match 更快?

    $a = '<page>
<username>someone</username>
  <id>3020778</id>
  <text xml:space="preserve"> The quick brown fox. </text>
</page>';

preg_match_all("(\<.+\>(.+)\<\/.+\>)U",$a, $r);    
?><pre><? print_r($r);?></pre><?

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

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