String Replacement

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I am not sure where to start on this on.

I have a string as follows '<text

The text identifier always remains the same ie. 3ece..... for each
article. The string is much longer containing several different
identifiers. I would like to be able to jump to the one shown, and
then extract the CDATA value shown after ie. 12345. The length of this
may change, so I need a way to extract the text between the second '['
and first ']' . I hope that makes sense.

Any advice would be greatly appreciated.


Re: String Replacement


Quoted text here. Click to load it

A regular expression might help, something like


It should match on the last '[' and then capture all following chars
which are not ']'.

But since this looks like XML, using DOM or SimpleXML might be an option
as well. Usually it's easier and the cleaner way to work with XML using
the appropriate tools.


Re: String Replacement

Alec wrote:
Quoted text here. Click to load it

Read up on XML processing.

Quoted text here. Click to load it

Assuming your actual data is well-formed XML (the above snippet is not):


  $dom = new DOMDocument;
  $xpath = new DOMXpath($dom);
  $query = '/text[starts-with(@identifier, '3ece')]/value/text()';
  $nodelist = $xpath->evaluate($query);
  if ($nodelist->length === 1)
    die('invalid query result: '.
        '['.strval($nodelist->length).'] nodes returned'."\n");


Pavel Lepin

Re: String Replacement

Firstly many thanks both of you for the quick reply.

XML processing is new to me, but I will try to get my head around


Re: String Replacement

Pavel given example of DOM parser usage for XML. Alternatively you can try =
to use SAX parser. It can look a bit more complex from programming point bu=
t it is fast and efficient by memory usage (critical for large XML). Also S=
AX parser able to process incorrectly formatted XML which is a problem for =

Site Timeline