如果你想远离 RegEx,你可以使用 strpos 和 substr 的丑陋组合来查找 "<a " 提取,等等。
像这样丑陋的东西(这只找到第一个链接,它会因错误代码而失败,并且找不到 ' (这是无效的 XHTML))这是个坏主意:
function findLink($string) {
$pos = strpos($string, "<a ");
if ($pos === false)
return false;
$string = substr($string, $pos);
$pos = strpos($string, "href");
$string = substr($string, $pos);
// First quote
$pos = strpos($string, '"');
$string = substr($string, $pos + 1);
// Last quote
$pos = strpos($string, '"');
$string = substr($string, 0, $pos);
if (trim($string) != "")
return $string;
return false;
}
我确定您也可以尝试对字符串进行标记?
$token = strtok($string, "\"'");
while ($token !== false) {
// Look for anything with a link in it
str_replace(array("http://", "www", "ftp://"), "", ltrim($string), $count);
if ($count > 0)
return $string;
$token = strtok($string, "\"'");
}
return false;
注意:这些不是很好的方法。