我有以下字符串:
$out = '
<li style="margin: 0px; padding: 0px; ">myspace <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> <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"> </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 <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> <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"> </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 <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> <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"> </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 <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> <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"> </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 <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> <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"> </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 <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> <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"> </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 <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> <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"> </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; ">
和之前
<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, ' <a href="http://www.goo', $startsAt);
$result = substr($out, $startsAt, $endsAt - $startsAt);
echo $result;
我知道,缺少一些 foreach 匹配的东西,但是由于我是 PHP 新手,我仍然坚持这一点,直到我完全理解这一切是如何工作的。我尝试了类似foreach($result as match) { echo $match; }
的东西,但没有成功。一定是我想念的东西。
不要对我太苛刻,我对 PHP 还是很陌生(大约一周前开始),但我愿意学习 :)
感谢您的时间。