Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #11032
| From | Pavel Lepin <p.lepin@dyninno.com> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: String Replacement |
| Date | 2011-02-07 10:17 +0200 |
| Organization | solani.org |
| Message-ID | <iio9qs$nnu$1@solani.org> (permalink) |
| References | <05c6ea05-cabb-41b7-9f9b-68587327e210@f2g2000yqf.googlegroups.com> |
Alec wrote:
> I am not sure where to start on this on.
Read up on XML processing.
> I have a string as follows '<text
> identifier="3ecebe84-8ddd-475e-86ca-300b5298fc34">
> <value><![CDATA[12345]]></value>'
>
> 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.
Assuming your actual data is well-formed XML (the above snippet is not):
<?php
$dom = new DOMDocument;
$dom->loadXML($string);
$xpath = new DOMXpath($dom);
$query = '/text[starts-with(@identifier, \'3ece\')]/value/text()';
$nodelist = $xpath->evaluate($query);
if ($nodelist->length === 1)
{
print($nodelist->item(0)->nodeValue."\n");
}
else
{
die('invalid query result: '.
'['.strval($nodelist->length).'] nodes returned'."\n");
}
?>
--
Pavel Lepin
Back to comp.lang.php | Previous | Next — Next in thread | Find similar | Unroll thread
Re: String Replacement Pavel Lepin <p.lepin@dyninno.com> - 2011-02-07 10:17 +0200 Re: String Replacement Alec <ajtdds@aol.com> - 2011-02-07 22:39 -0800
csiph-web