Home > php教程 > php手册 > body text

文本转换超链接函数

WBOY
Release: 2016-06-07 11:34:56
Original
1027 people have browsed it

文本转换超链接函数:
本文以联盟http://www.heimaolianmeng.com/heimaoseojishu/该篇文章为例,将描述中的文本链接转换成可点击的超链接,并且以新窗口打开。

function urlToLink($str) {
$arr = array("www." => "http://www.");
$str = strtr($str, $arr);
$arr = array("http://http://" => "http://");
$str = strtr($str, $arr);
$str2 = explode("http://", $str);
for ($n = 1; isset($str2[$n]); $n ++) {
$str3 = explode(".", $str2[$n]);
if (isset($str3[1])) {
$str4 = explode("www.", $str2[$n]);
if ((isset($str4[1]) && isset($str3[2])) || !isset($str4[1])) {
$length = strlen($str2[$n]);
for ($i = 0; $i //从空格断开
if (($i - 1) == strlen(trim(mb_substr($str2[$n], 0, $i, 'gb2312')))) {
$ii = $i - 1;
$url1 = mb_substr($str2[$n], 0, $ii, 'gb2312');
$url2 = mb_substr($str2[$n], $ii, $length, 'gb2312');
$url3 = "http://" . $url1 . "" . $url2;
break;
}

//从出现汉字处断开
if ($i != strlen(mb_substr($str2[$n], 0, $i, 'gb2312'))) {
$ii = $i - 1;
$url1 = mb_substr($str2[$n], 0, $ii, 'gb2312');
$url2 = mb_substr($str2[$n], $ii, $length, 'gb2312');
$url3 = "http://" . $url1 . "" . $url2;
break;
}
if ($i == $length)
$url3 = "http://" . $str2[$n] . "";
}
} else
$url3 = "http://" . $str2[$n];
} else
$url3 = "http://" . $str2[$n];
$url .= $url3;
}
if (substr($str, 0, 7) == "http://")
$url = "" . $str2[0] . "" . $url;
else
$url = $str2[0] . $url;
return $url;
}

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template