Reputation: 80485
I need help with getting a part of an XML file with tags like this:
<SomeTag><![CDATA[TEXT I WANT HERE]]></SomeTag>
I've been playing around with RexExp for this, and can't get it right. Can you suggest the proper way please?
EDIT:
Not interested in XML parsing for this particular case.
can be anything, not just "SomeTag". Same with "Text I want here".
Thank you.
Upvotes: 0
Views: 112
Reputation: 5097
With http://regexp.zug.fr/ I write un 5 sec a very simple pattern
preg_match_all("`<!\[CDATA\[(.*?)\]\]>`U", $source, $matches);
Upvotes: 2
Reputation: 20343
You don't even need RegEx for this. Simple strpos
is enough:
$start = strpos ($text, '<SomeTag><![CDATA[');
$end = strpos ($text, ']]></SomeTag>', $start);
return substr ($text, $start, $end - $start);
Upvotes: 0
Reputation: 9713
As everibody else suggested use an xml parser for this job. The code bellow will show you how to do it with regex but it's not the proper way of doing things!
$string = '<SomeTag><![CDATA[TEXT I WANT HERE]]></SomeTag>';
preg_match_all('/<sometag><\!\[CDATA\[(.*)\]\]><\/sometag>/i', $string, $matches);
var_dump($matches);
preg_match_all('/<\!\[CDATA\[(.*)\]\]>/', $string, $matches);
var_dump($matches);
Upvotes: 0