PHP:字符串中的echo字,其中start和end始终相同

时间:2012-10-12 14:08:29

标签: php

我有以下字符串:

$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的 谷歌 易趣 雅虎 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的新手(一周前开始)但我愿意学习:)

感谢您的时间。

3 个答案:

答案 0 :(得分: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
)

答案 1 :(得分: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);
?>

答案 2 :(得分:0)

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

PHP内置的DOM functions也许可以帮助...