如何在php中超链接网址或非网址

如何在php中超链接网址或非网址,php,Php,我有一个网站是用户输入文章和他们的参考资料(比如维基百科)。保存在数据库中的引用包括网址和非网址。目前,我正在使用google的search?q和它的正常工作超链接脚本 echo("<br><a rel=nofollow target=_blank href='http://www.google.com/search?q=".urlencode($row['ref'])."' class=art>$row[ref]</a>"); 或者,如果用户按

我有一个网站是用户输入文章和他们的参考资料(比如维基百科)。保存在数据库中的引用包括网址和非网址。目前,我正在使用google的search?q和它的正常工作超链接脚本

     echo("<br><a rel=nofollow  target=_blank href='http://www.google.com/search?q=".urlencode($row['ref'])."' class=art>$row[ref]</a>");
或者,如果用户按以下方式输入引用

     washingtonpost+sports+capitals
它应该去谷歌搜索


提前感谢您的帮助

您可以检查是否存在
://
以查看输入的数据是否为链接。它并不完美,但您可以调整它以满足您的需要:

$URL = 'http://www.google.com?/q=' . urlencode($Reference);
if (strpos($Reference, '://') !== false)
{
    $URL = $Reference;
}

echo '<a href="' . $Reference . '">' . $Reference . '</a>';
$URL='1!'http://www.google.com?/q=' . urlencode($Reference);
if(strpos($Reference,“:/”)!==false)
{
$URL=$Reference;
}
回声';

不可能自动将您的引用检测为网址或非网址。您必须检查引用是否为URL

function isValidURL($url) {
  return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}

您可以使用常规表达式查看它是否为链接,并使其成为链接。正则表达式还确保它是链接的有效语法

 $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
 // The Text you want to filter for urls
 $text = "http://www.google.com"; #The text you want to filter goes here
 // Check if there is a url in the text
 if(preg_match($reg_exUrl, $text, $url)) {
   // make the urls hyper links
   echo preg_replace($reg_exUrl, "<a href="{$url[0]}">{$url[0]}</a> ", $text);
 } else {
   // if no urls in the text just return the text
   echo '<a href="http://www.google.com/search?q=',urlencode($text),'">',$text,'</a>';
 }
$reg|u exUrl=“/(http | https | ftp | ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/”;
//要筛选URL的文本
$text=”http://www.google.com"; #要筛选的文本将显示在此处
//检查文本中是否有url
if(preg_match($reg_exUrl,$text,$url)){
//使URL成为超链接
echo preg_替换($reg_exUrl,“,$text);
}否则{
//如果文本中没有URL,请返回文本
回声';
}
 $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
 // The Text you want to filter for urls
 $text = "http://www.google.com"; #The text you want to filter goes here
 // Check if there is a url in the text
 if(preg_match($reg_exUrl, $text, $url)) {
   // make the urls hyper links
   echo preg_replace($reg_exUrl, "<a href="{$url[0]}">{$url[0]}</a> ", $text);
 } else {
   // if no urls in the text just return the text
   echo '<a href="http://www.google.com/search?q=',urlencode($text),'">',$text,'</a>';
 }