开发者

How to find a particular value from a string

开发者 https://www.devze.com 2023-01-14 03:13 出处:网络
I have a string (not xml ) <headername>X-Mailer-Recptid</headername> <headervalue>15772348</headervalue>

I have a string (not xml )

<headername>X-Mailer-Recptid</headername>
<headervalue>15772348</headervalue>
</header>

from th开发者_如何学JAVAis, 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);
?>


// 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)


       <?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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号