ralpu
ralpu

Reputation: 205

php preg_match pattern for parsing tags

I want to know a good preg_match pattern in php for extracting data between tags.

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".

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?

Upvotes: 3

Views: 8200

Answers (1)

sukinsan
sukinsan

Reputation: 513

    $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><?

Upvotes: 1

Related Questions