3

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?

4
  • There are some things that regular expressions are very good at. IMNSHO, parsing XML isn't one of them. Use an XML parser and get your data that way :) Commented Jul 5, 2011 at 9:46
  • 2
    Typically speaking, parsing your XML documents is almost always more reliable than using regular expressions. Commented Jul 5, 2011 at 9:47
  • This one sums it up nicely: stackoverflow.com/questions/1732348/… Commented Jul 5, 2011 at 9:48
  • 1
    $page = simplexml_load_string($xml); echo $page->username, $page->id, $page->text; Also see A simple program to CRUD Node values of an XML file, Best XML Parser for PHP and Best Methods to parse HTML Commented Jul 5, 2011 at 10:02

1 Answer 1

1
    $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><?
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.