0

I have this XML content:

$XML = "<item xmlns='http://...'>Some value</item>";

I want to extract the value off the tag, so I use the following regular expression:

$Value = preg_replace("/^<item [.]+>/","",$XML);
$Value = preg_replace("/</item>$/","",$Value);

But these don't work, the $Value finally is still the same as $XML. How to fix this?

3
  • 1
    Why not use a proper XML parser instead? Commented Nov 21, 2011 at 22:27
  • coz my xml content has only 1 tag Commented Nov 21, 2011 at 22:28
  • 1
    Using regular expression to parse XML can lead to unespected results. For exemple, if a string with a "&" character is a possible value for the item element, the "&" is escaped as &amp; in well formed XML and the regular expression won't get rid of this entity. Commented Nov 21, 2011 at 22:57

1 Answer 1

2

If you want to extract the text inside the tag you can use this code:

$string = "<item xmlns='http://...'>Some value</item>";
$regex = '#<.*?>(.*?)</.*?>#';
preg_match_all($regex, $string, $matches);
$matches = $matches[1][0];
echo $matches;

If you want to use preg_replace, use this code:

$Value = preg_replace("/^<item .+?>/","",$XML);
$Value = preg_replace("/<\/item>$/","",$Value);
Sign up to request clarification or add additional context in comments.

3 Comments

im i mistaken between posix regex & perl-compatible regex?
@PaulDinh This regex: <.*?>(.*?)</.*?> is compatible with every language(if you need, you have to escape the '/' with '\/').
pls how to do the same thing with preg_replace instead of preg_match_all. the xml has only 1 tag.

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.