开发者

Get text between complex tags in PHP

I'm trying to get text from in between 2 html tags, only the difficulty is that the tag can differ from name.

I'll explain into detail:

<icon di开发者_Python百科splayid="62115">inv_helmet_130</icon>

I have to get the

inv_helmet_130

But the displayid of the tag can differ, any ideas on how to solve this? Perhaps with a regular explression but I'm not good at those.

Thanks in advance.


Simple HTML DOM Parser should be able to handle that:

$html = file_get_html('http://www.example.com/');

foreach($html->find('icon') as $element) 
       echo $element->innertext . '<br>';

You can single them out like this:

echo $html->find('icon', 0)->innertext; // get me the first


Do not use a regular expressions for dealing with HTML.

Use something specifically designed for XML/HTML like XPath instead. It is already part of PHP's libraries.

The XPath expression you want is going to something along the lines of this,

//icon/text()

Which reads, "Select the text from any icon element in the document, regardless of its id or parents."


Since <icon> is not a valid HTML tag, I presume you're working with XML or some other markup language. PHP has a pretty handy extension to deal with XML:

<?php

$xml = simplexml_load_string('<?xml version="1.0"?><icon displayid="62115">inv_helmet_130</icon>');
echo (string)$xml[0];

?>

The exact code, of course, depends on your exact string. As suggested, an xpath search can do the trick.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜