I have a string (not xml )
<headername>X-Mailer-Recptid</headername>
<headervalue>15772348</headervalue>
</header>
from this, i need to get the value 15772348
, that is the value of headervalue
. How is possible?
I have a string (not xml )
<headername>X-Mailer-Recptid</headername>
<headervalue>15772348</headervalue>
</header>
from this, i need to get the value 15772348
, that is the value of headervalue
. How is possible?
Use PHP DOM and traverse the headervalue
tag using getElementsByTagName()
:
<?php
$doc = new DOMDocument;
@$doc->loadHTML('<headername>X-Mailer-Recptid</headername><headervalue>15772348</headervalue></header>');
$items = $doc->getElementsByTagName('headervalue');
for ($i = 0; $i < $items->length; $i++) {
echo $items->item($i)->nodeValue . "\n";
}
?>
This gives the following output:
15772348
[EDIT]: Code updated to suppress non-HTML warning about invalid headername and headervalue tags as they are not really HTML tags. Also, if you try to load it as XML, it totally fails to load.
This looks XML-like to me. Anyway, if you don't want to parse the string as XML (which might be a good idea), you could try something like this:
<?
$str = "<headervalue>15772348</headervalue>";
preg_match("/<headervalue\>([0-9]+)<\/headervalue>/", $str, $matches);
print_r($matches);
?>