【发布时间】:2012-12-21 19:57:09
【问题描述】:
我有以下代码 sn-p,它基本上解析我的博客站点并将一些信息存储为变量:
global $articles;
$items = $html->find('div[class=blogpost]');
foreach($items as $post) {
$articles[] = array($post->children(0)->innertext,
$post->children(1)->first_child()->outertext);
}
foreach($articles as $item) {
echo $item[0];
echo $item[1];
echo "<br>";
}
以上代码输出如下:
Title of blog post 1 <script type="text/javascript">execute_function(3,'')</script><a href="http://www.example.com/cool_news" id="963" target="_blank" >Click here for news</a> <img src="/news.gif" width="12" height="12" title="validated" /><span class="title">
Title of blog post 2 <script type="text/javascript">execute_function(3,'')</script><a href="http://www.example.com/neato" id="963" target="_blank" >Click here for neato</a> <img src="/news.gif" width="12" height="12" title="validated" /><span class="title">
Title of blog post 3 <script type="text/javascript">execute_function(3,'')</script><a href="http://www.example.com/lame" id="963" target="_blank" >Click here for lame</a> <img src="/news.gif" width="12" height="12" title="validated" /><span class="title">
$item[0] 包含“博客文章 X 的标题”,$item[1] 包含其余部分。
我想要做的是解析 $item[1] 并仅保留其中包含的 URL 作为单独的变量。也许我没有正确地表达我的问题,但我找不到任何可以帮助我解决这个问题的东西。
谁能帮帮我?
【问题讨论】:
-
使用 preg_match 和
preg_match("href=\"(.*?)\"si", $source, $match);之类的东西来获取字符串中的 href 值。 -
您已经在使用适当的解析器解析 HTML。您想继续解析 标记。你现在做对了。不要使用正则表达式!
-
问题是我不知道如何进一步解析它。我使用的解析器似乎不再受支持:net.tutsplus.com/tutorials/php/…
标签: php arrays variables html-parsing extract