Home > Backend Development > PHP Tutorial > Simplexml_load_string usage example sharing in php_PHP tutorial

Simplexml_load_string usage example sharing in php_PHP tutorial

WBOY
Release: 2016-07-13 10:37:35
Original
934 people have browsed it

First use a piece of code to reproduce the problem

At first glance, the results are puzzling:

Copy code The code is as follows:

$string = <<
hello
world

EOF;

$data = simplexml_load_string($string);

print_r($data);
print_r($data->foo);
?>

At first glance, the results are puzzling:

Copy code The code is as follows:

SimpleXMLElement Object
(
[foo] => Array
(
[0] => SimpleXMLElement Object
(
[bar] => hello
)
[1] => SimpleXMLElement Object
(
[ bar] => world
)
)
)
SimpleXMLElement Object
(
[bar] => hello
)

Obviously print_r shows that foo is an array with two bar elements, but in the end only one bar element is displayed!
The reason is actually very simple. In the result of simplexml_load_string shown above, foo is not an array, but an iterable object!
You can confirm like this:

Copy code The code is as follows:

foreach ($data->foo as $v) print_r($v);
foreach ($data->children() as $v) print_r($v);

It seems that the appearance of print_r or var_dump is not completely trustworthy, so please pay more attention to it.

Suppose the XML data we obtain is as follows: (can be obtained using curl, fsockopen, etc.)

Copy code The code is as follows:


Hello

Array;Array;Array;

Haven't seen you for a long time. How are you? I haven’t seen you for a few days, how are you?


Hello! How are you?
Hey, hello?


Hello, Brooks! How are you?
Hello, Brooks ! Are you OK?


Hi, Barbara, how are you?
Hi, Barbara Barra, how are you?


How are you? -Quite well, thank you.
Are you OK? -Very well, thank you.


Get through simplexml_load_string:

Copy code The code is as follows:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [num] => 219
            [id] => 219
            [name] => 219
        )

    [key] => 你好
    [pos] => SimpleXMLElement Object
        (
        )

    [acceptation] => Array;Array;Array;
    [sent] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [orig] => Haven't seen you for a long time. How are you?
                    [trans] => 多日不见了,你好吗?
                )

            [1] => SimpleXMLElement Object
                (
                    [orig] => Hello! How are you?
                    [trans] => 嘿,你好?
                )

            [2] => SimpleXMLElement Object
                (
                    [orig] => Hello, Brooks!How are you?
                    [trans] => 喂,布鲁克斯!你好吗?
                )

            [3] => SimpleXMLElement Object
                (
                    [orig] => Hi, Barbara, how are you?
                    [trans] => 嘿,芭芭拉,你好吗?
                )

            [4] => SimpleXMLElement Object
                (
                    [orig] => How are you? -Quite well, thank you.
                    [trans] => 你好吗?-很好,谢谢你。
                )

        )

)

我们在PHP语言中可以用以下方法取得我们想要的值:

复制代码 代码如下:

$data = <<

 你好
 
 Array;Array;Array;
 
  Haven't seen you for a long time. How are you?
  多日不见了,你好吗?
 

 
  Hello! How are you?
  嘿,你好?
 

 
  Hello, Brooks!How are you?
  喂,布鲁克斯!你好吗?
 

 
  Hi, Barbara, how are you?
  嘿,芭芭拉,你好吗?
 

 
  How are you? -Quite well, thank you.
  你好吗?-很好,谢谢你。
 


XML;
$xmldata = simplexml_load_string($data);
header("Content-Type: text/html; charset=UTF-8");
print_r($xmldata);
echo "
".trim($xmldata->sent[0]->orig); //Haven't seen you for a long time. How are you?
echo "
".trim($xmldata->key); //你好
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/735236.htmlTechArticle先用一段代码重现一下问题 乍一看,结果很让人费解: 复制代码 代码如下: ?php $string = EOF data foobarhello/bar/foo foobarworld/bar/foo /data EOF; $dat...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template