我有一个功能,将在链接之前添加<a href>
标记,链接之后添加</a>
。但是,对于某些网页而言,它会中断。你将如何改进这个功能?谢谢!PHP - 将链接添加到字符串中的URL
function processString($s)
{
// check if there is a link
if(preg_match("/http:\/\//",$s))
{
print preg_match("/http:\/\//",$s);
$startUrl = stripos($s,"http://");
// if the link is in between text
if(stripos($s," ",$startUrl)){
$endUrl = stripos($s," ",$startUrl);
}
// if link is at the end of string
else {$endUrl = strlen($s);}
$beforeUrl = substr($s,0,$startUrl);
$url = substr($s,$startUrl,$endUrl-$startUrl);
$afterUrl = substr($s,$endUrl);
$newString = $beforeUrl."<a href=\"$url\">".$url."</a>".$afterUrl;
return $newString;
}
return $s;
}
正则表达式是有点草率,但我投入的99%,将有正确的网址,如果有的话 – AlexBrand 2010-11-18 16:53:15
它打破什么网页? – 2010-11-18 16:54:15
开始时你也会测试https,但稍后会忽略“s”。不知道,如果这导致这个错误,因为我也不知道,哪些页面被破坏;) – KingCrunch 2010-11-18 16:58:39