从XML之类的数据中提取节点,不需要额外的PHP libs

时间:2023-01-11 16:04:04

I am returned the following:

我得到的答复如下:

<links>
    <image_link>http://img357.imageshack.us/img357/9606/48444016.jpg</image_link>
    <thumb_link>http://img357.imageshack.us/img357/9606/48444016.th.jpg</thumb_link>
    <ad_link>http://img357.imageshack.us/my.php?image=48444016.jpg</ad_link>
    <thumb_exists>yes</thumb_exists>
    <total_raters>0</total_raters>
    <ave_rating>0.0</ave_rating>
    <image_location>img357/9606/48444016.jpg</image_location>
    <thumb_location>img357/9606/48444016.th.jpg</thumb_location>
    <server>img357</server>
    <image_name>48444016.jpg</image_name>
    <done_page>http://img357.imageshack.us/content.php?page=done&amp;l=img357/9606/48444016.jpg</done_page>
    <resolution>800x600</resolution>
    <filesize>38477</filesize>
    <image_class>r</image_class>
</links>

I wish to extract the image_link in PHP as simply and as easily as possible. How can I do this?

我希望在PHP中尽可能简单和容易地提取image_link。我该怎么做呢?

Assume, I can not make use of any extra libs/plugins for PHP. :)

假设,我不能为PHP使用任何额外的lib /plugins。:)

Thanks all

感谢所有

3 个解决方案

#1


4  

At Josh's answer, the problem was not escaping the "/" character. So the code Josh submitted would become:

在Josh的回答中,问题并没有逃过“/”的角色。Josh提交的代码会变成:

$text = 'string_input';
preg_match('/<image_link>([^<]+)<\/image_link>/gi', $text, $regs);
$result = $regs[0];

Taking usoban's answer, an example would be:

以usoban的回答为例:

<?php

// Load the file into $content

$xml = new SimpleXMLElement($content) or die('Error creating a SimpleXML instance');

$imagelink = (string) $xml->image_link; // This is the image link

?>

I recommend using SimpleXML because it's very easy and, as usoban said, it's builtin, that means that it doesn't need external libraries in any way.

我推荐使用SimpleXML,因为它非常简单,正如usoban所说,它是builtin,这意味着它不需要任何外部库。

#2


3  

You can use SimpleXML as it is built in PHP.

您可以使用SimpleXML,因为它是在PHP中构建的。

#3


1  

use regular expressions

使用正则表达式

$text = 'string_input';
preg_match('/<image_link>([^<]+)</image_link>/gi', $text, $regs);
$result = $regs[0];

#1


4  

At Josh's answer, the problem was not escaping the "/" character. So the code Josh submitted would become:

在Josh的回答中,问题并没有逃过“/”的角色。Josh提交的代码会变成:

$text = 'string_input';
preg_match('/<image_link>([^<]+)<\/image_link>/gi', $text, $regs);
$result = $regs[0];

Taking usoban's answer, an example would be:

以usoban的回答为例:

<?php

// Load the file into $content

$xml = new SimpleXMLElement($content) or die('Error creating a SimpleXML instance');

$imagelink = (string) $xml->image_link; // This is the image link

?>

I recommend using SimpleXML because it's very easy and, as usoban said, it's builtin, that means that it doesn't need external libraries in any way.

我推荐使用SimpleXML,因为它非常简单,正如usoban所说,它是builtin,这意味着它不需要任何外部库。

#2


3  

You can use SimpleXML as it is built in PHP.

您可以使用SimpleXML,因为它是在PHP中构建的。

#3


1  

use regular expressions

使用正则表达式

$text = 'string_input';
preg_match('/<image_link>([^<]+)</image_link>/gi', $text, $regs);
$result = $regs[0];