1

我有以下字符串:

$out = '
<li style="margin: 0px; padding: 0px; ">myspace&nbsp;<a href="http://www.google.com/search?hl=en&lr=&q=myspace" rel="gb_pageset[]" title="Results @ Google" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/g-tiny.jpg" alt="Results @ Google" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a>&nbsp;<a href="http://search.yahoo.com/search?p=myspace" rel="gb_pageset[]" title="Results @ Yahoo" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/y-tiny.jpg" alt="Results @ Yahoo" border="0" style="margin: 0px; padding: 0px; " width="16" height="16">&nbsp;</a><a href="http://search.msn.com/results.aspx?FORM=MSNH&srch_type=0&q=myspace" rel="gb_pageset[]" title="Results @ MSN" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/m-tiny.jpg" alt="Results @ MSN" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a></li>
<li style="margin: 0px; padding: 0px; ">google&nbsp;<a href="http://www.google.com/search?hl=en&lr=&q=google" rel="gb_pageset[]" title="Results @ Google" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/g-tiny.jpg" alt="Results @ Google" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a>&nbsp;<a href="http://search.yahoo.com/search?p=google" rel="gb_pageset[]" title="Results @ Yahoo" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/y-tiny.jpg" alt="Results @ Yahoo" border="0" style="margin: 0px; padding: 0px; " width="16" height="16">&nbsp;</a><a href="http://search.msn.com/results.aspx?FORM=MSNH&srch_type=0&q=google" rel="gb_pageset[]" title="Results @ MSN" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/m-tiny.jpg" alt="Results @ MSN" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a></li>
<li style="margin: 0px; padding: 0px; ">youtube&nbsp;<a href="http://www.google.com/search?hl=en&lr=&q=youtube" rel="gb_pageset[]" title="Results @ Google" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/g-tiny.jpg" alt="Results @ Google" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a>&nbsp;<a href="http://search.yahoo.com/search?p=youtube" rel="gb_pageset[]" title="Results @ Yahoo" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/y-tiny.jpg" alt="Results @ Yahoo" border="0" style="margin: 0px; padding: 0px; " width="16" height="16">&nbsp;</a><a href="http://search.msn.com/results.aspx?FORM=MSNH&srch_type=0&q=youtube" rel="gb_pageset[]" title="Results @ MSN" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/m-tiny.jpg" alt="Results @ MSN" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a></li>
<li style="margin: 0px; padding: 0px; ">ebay&nbsp;<a href="http://www.google.com/search?hl=en&lr=&q=ebay" rel="gb_pageset[]" title="Results @ Google" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/g-tiny.jpg" alt="Results @ Google" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a>&nbsp;<a href="http://search.yahoo.com/search?p=ebay" rel="gb_pageset[]" title="Results @ Yahoo" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/y-tiny.jpg" alt="Results @ Yahoo" border="0" style="margin: 0px; padding: 0px; " width="16" height="16">&nbsp;</a><a href="http://search.msn.com/results.aspx?FORM=MSNH&srch_type=0&q=ebay" rel="gb_pageset[]" title="Results @ MSN" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/m-tiny.jpg" alt="Results @ MSN" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a></li>
<li style="margin: 0px; padding: 0px; ">yahoo&nbsp;<a href="http://www.google.com/search?hl=en&lr=&q=yahoo" rel="gb_pageset[]" title="Results @ Google" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/g-tiny.jpg" alt="Results @ Google" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a>&nbsp;<a href="http://search.yahoo.com/search?p=yahoo" rel="gb_pageset[]" title="Results @ Yahoo" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/y-tiny.jpg" alt="Results @ Yahoo" border="0" style="margin: 0px; padding: 0px; " width="16" height="16">&nbsp;</a><a href="http://search.msn.com/results.aspx?FORM=MSNH&srch_type=0&q=yahoo" rel="gb_pageset[]" title="Results @ MSN" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/m-tiny.jpg" alt="Results @ MSN" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a></li>
<li style="margin: 0px; padding: 0px; ">craigslist&nbsp;<a href="http://www.google.com/search?hl=en&lr=&q=craigslist" rel="gb_pageset[]" title="Results @ Google" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/g-tiny.jpg" alt="Results @ Google" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a>&nbsp;<a href="http://search.yahoo.com/search?p=craigslist" rel="gb_pageset[]" title="Results @ Yahoo" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/y-tiny.jpg" alt="Results @ Yahoo" border="0" style="margin: 0px; padding: 0px; " width="16" height="16">&nbsp;</a><a href="http://search.msn.com/results.aspx?FORM=MSNH&srch_type=0&q=craigslist" rel="gb_pageset[]" title="Results @ MSN" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/m-tiny.jpg" alt="Results @ MSN" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a></li>
<li style="margin: 0px; padding: 0px; ">you tube&nbsp;<a href="http://www.google.com/search?hl=en&lr=&q=you%20tube" rel="gb_pageset[]" title="Results @ Google" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/g-tiny.jpg" alt="Results @ Google" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a>&nbsp;<a href="http://search.yahoo.com/search?p=you%20tube" rel="gb_pageset[]" title="Results @ Yahoo" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/y-tiny.jpg" alt="Results @ Yahoo" border="0" style="margin: 0px; padding: 0px; " width="16" height="16">&nbsp;</a><a href="http://search.msn.com/results.aspx?FORM=MSNH&srch_type=0&q=you%20tube" rel="gb_pageset[]" title="Results @ MSN" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/m-tiny.jpg" alt="Results @ MSN" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a></li>
';

并且基本上想要回显之后显示的所有内容

<li style="margin: 0px; padding: 0px; ">

和之前

&nbsp;<a href="http://www.goo

我想看到的结果是:

myspace 谷歌 ebay 雅虎 craigslist 你管

我尝试了在 stackoverflow 上找到的各种方法,但由于某种原因,它要么只返回一个单词,要么出现 500 服务器错误,所以也许你知道正确的解决方案。

例如:

$startsAt = strpos($out, '<li style="margin: 0px; padding: 0px; ">') + strlen('<li style="margin: 0px; padding: 0px; ">');
$endsAt = strpos($out, '&nbsp;<a href="http://www.goo', $startsAt);
$result = substr($out, $startsAt, $endsAt - $startsAt);

echo $result;

我知道,缺少一些 foreach 匹配的东西,但是由于我是 PHP 新手,我仍然坚持这一点,直到我完全理解这一切是如何工作的。我尝试了类似foreach($result as match) { echo $match; }的东西,但没有成功。一定是我想念的东西。

不要对我太苛刻,我对 PHP 还是很陌生(大约一周前开始),但我愿意学习 :)

感谢您的时间。

4

3 回答 3

2
if (preg_match_all('/>([^<>]+?)&nbsp;/', $out, $matches)) {
    print_r($matches[1]);
}

这将捕获 和 之间的>文本&nbsp;。该部分匹配除了重复一次或多次的([^<>]+?)尖括号之外的任何字符,找到可能的最短匹配项。括号用于捕获这些匹配的部分,以便我们之后可以访问它们。[^<>]+?$matches[1]

输出:

Array
(
    [0] => myspace
    [1] => google
    [2] => youtube
    [3] => ebay
    [4] => yahoo
    [5] => craigslist
    [6] => you tube
)
于 2012-10-12T14:17:01.647 回答
1

您可以在这里做一些事情...explode通过换行符(将您的<li>..</li>行作为一个数组或我们一个正则表达式 - 这当然有一些学习曲线。你的想法会起作用(几乎在那里)但是它依赖于以某种方式精确格式化的东西——有几种方法可以避免产生相同的结果。

<?php    
$out=/*...*/
function findStart($string,$last=0) {
   $start=strpos($string,"<li",$last);
   if ($start===false) return -1;//No new start
   $start=strpos($string,">",$start);
   if ($start===false) return -1;//Mal formed <li>?
   return $start+1;//Don't include the >
}

$start=0;
while (0<$start=findStart($out,$start)) {
   $end=strpos($out,"&nbsp;<",$start);
   if ($end!==false) {
     $set[]=substr($out,$start,$end-$start);
     $start=$end;//Forward the pointer for the next loop
   } else {$start=-1;}
}

//Now $set is an array of the values
print_r($set);
?>
于 2012-10-12T14:22:23.643 回答
0

您是否希望使用 PHP 解析 HTML,以便在 LI 中获得所有内容(甚至可能是嵌套的 HTML 元素)?问题是您可以在另一个 LI 中拥有相同的 LI 代码,因此使用字符串函数可能会很棘手。

也许PHP 中内置的DOM 函数可以在这里提供帮助……

于 2012-10-12T14:15:26.657 回答