2

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?

dertkw
  • 7,798
  • 5
  • 37
  • 45
Linto P D
  • 8,839
  • 7
  • 30
  • 39
  • 3
    You say you do not have xml, but what you present is definitely some kind of XML structured code. So if you have a XML structured document you should use the relying parser. – Julius F Sep 03 '10 at 14:34
  • Could it be, that you have a HTTP Header, and show it as XML to us, but you actually talk about a HTTP Header? – Julius F Sep 03 '10 at 15:30

4 Answers4

10

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.

shamittomar
  • 46,210
  • 12
  • 74
  • 78
  • it output the answer, but also getting some warning Warning: DOMDocument::loadHTML() [function.DOMDocument-loadHTML]: Tag headername invalid in Entity, line: 1 in C:\wamp\www\test.php on line 496 Warning: DOMDocument::loadHTML() [function.DOMDocument-loadHTML]: Tag headervalue invalid in Entity, line: 1 in C:\wamp\www\test.php on line 496 Warning: DOMDocument::loadHTML() [function.DOMDocument-loadHTML]: Unexpected end tag : header in Entity, line: 1 in C:\wamp\www\test.php on line 496 – Linto P D Sep 03 '10 at 14:41
  • @Linto, I have updated the code to suppress the warning. Try new code now. – shamittomar Sep 03 '10 at 14:44
  • It will fail to load as XML because there is no root element. – BoltClock Sep 03 '10 at 18:59
  • @Boltclock: yes, that's why I made it clear by editing the edit :) – shamittomar Sep 03 '10 at 19:01
2

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);
?>
Michael Mior
  • 28,107
  • 9
  • 89
  • 113
  • 3
    It's ok for simple things like this, but obligatory warning about using regular expressions to parse non-regular languages. [That way lies madness](http://stackoverflow.com/questions/1732348/1732454#1732454). – James Sep 03 '10 at 14:42
1

// find string short way

function my_url_search($se_action_data)
    {
        // $regex = '/https?\:\/\/[^\" ]+/i';
           $regex="/<headervalue\>([0-9]+)<\/headervalue>/"
         preg_match_all($regex, $se_action_data, $matches);
         $get_url=array_reverse($matches[0]);
         return array_unique($get_url);
    }
echo my_url_search($se_action_data)
1
       <?php
      $html = new simple_html_dom();
        $html = str_get_html("<headername>X-Mailer-Recptid</headername>headervalue>15772348</headervalue></header>");        // Use Html dom here 
        $get_value=$html->find("headervalue", 0)->plaintext;
        echo $get_value;

    ?>



  http://simplehtmldom.sourceforge.net/manual.htm#section_find