修改函数以将文本 URL 转换为链接并限制名称
Modifying a function to convert text URLs to links and limit name
所以我有这个功能可以将文本 URLs 转换为链接。我需要添加什么到 preg_replace
,以限制长 URL,目前它只显示域,我想添加一个 第 4 个属性 , 显示完全 URL 的限制,限制 URL,我不知道该怎么做。此功能已经过测试并且运行良好,这就是我想继续使用它的原因,如何为第 4 个属性添加这个额外的代码段。
function autolink($str, $attributes=array()) {
$attrs = '';
foreach ($attributes as $attribute => $value) {
$attrs .= " {$attribute}=\"{$value}\"";
}
$str = ' ' . $str;
$str = preg_replace(
'|([\w\d]*)\s(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i',
' <a href=""'.$attrs.' title="" target="_blank" rel="nofollow"></a>',
$str
);
return $str;
}
谁能帮帮我。
输入:
$cont=autolink("https://www.example.com/test.php?ss=a https://www.example.com/2 https://www.exxxxxxxxxxxxxxxxxxaaample.com/3");
输出电流:
<a href="https://www.example.com/test.php?ss=a" title="https://www.example.com/test.php?ss=a" target="_blank" rel="nofollow">www.example.com</a> <a href="https://www.example.com/2" title="https://www.example.com/2" target="_blank" rel="nofollow">www.example.com</a> <a href="https://www.exxxxxxxxxxxxxxxxxxaaample.com/3" title="https://www.exxxxxxxxxxxxxxxxxxaaample.com/3" target="_blank" rel="nofollow">www.exxxxxxxxxxxxxxxxxxaaample.com</a>
想要的输出:
<a href="https://www.example.com/test.php?ss=a" title="https://www.example.com/test.php?ss=a" target="_blank" rel="nofollow">www.example.com/test...</a> <a href="https://www.example.com/2" title="https://www.example.com/2" target="_blank" rel="nofollow">www.example.com/2</a> <a href="https://www.exxxxxxxxxxxxxxxxxxaaample.com/3" title="https://www.exxxxxxxxxxxxxxxxxxaaample.com/3" target="_blank" rel="nofollow">www.exxxxxxxxxxxxx.....</a>
我应该可以将 URL 名称限制为 40 个字符。无论如何,第三个属性不需要并且会被修改,不返回域,但 URL 的真实名称限制为 40 个字符。
我希望它是最终的:答案是 *preg_replace_callback*
,这是最好的正则表达式,在这里使用,使文本 URL 到 HTML link 的最佳方法,简单修改。
function autolink($textOriginal)
{
$textOriginal=" $textOriginal";
$urlPattern = '|([\w]*)\s(https?://([\w.-]+\.[\w.]{2,6})[^\s\]\[\<\>]*/?)|i';
$textNew = preg_replace_callback($urlPattern, "replaceLinkParts", $textOriginal);
return $textNew;
}
function replaceLinkParts($matches)
{
$thefullurl=''.$matches[2].'';
$whatisthis=''.$matches[1].'';
//////////
$myDomainx = parse_url("$thefullurl");
$simpleurl="" . $myDomainx["host"] . "";
$simpleurlp = "" . $myDomainx["path"] . "";
if ($simpleurlp == "/") {
$simpleurlp = "";
}
$newurl="$simpleurl$simpleurlp";
////////////////
$lastpart= "";
$limichars="30";
$limitlast="10";
$calcit=$limichars-$limitlast;
$nmre=strlen($newurl);
if ($nmre > $limichars) {
$lastpart=$nmre - $limitlast;
$lastpart= substr($newurl, $lastpart, $limitlast);
$newurl= substr($newurl, 0, $calcit);
$newurl= ''.$newurl.'...';
}
////////////
$retthis=''.$whatisthis.' <a target="_blank" title="'.$thefullurl.'" rel="nofollow" href="'.$thefullurl.'">'.$newurl.''.$lastpart.'</a>';
return $retthis;
}
输出HTMLlink:www.example.com...ifverylongurl
所以我有这个功能可以将文本 URLs 转换为链接。我需要添加什么到 preg_replace
,以限制长 URL,目前它只显示域,我想添加一个 第 4 个属性 , 显示完全 URL 的限制,限制 URL,我不知道该怎么做。此功能已经过测试并且运行良好,这就是我想继续使用它的原因,如何为第 4 个属性添加这个额外的代码段。
function autolink($str, $attributes=array()) {
$attrs = '';
foreach ($attributes as $attribute => $value) {
$attrs .= " {$attribute}=\"{$value}\"";
}
$str = ' ' . $str;
$str = preg_replace(
'|([\w\d]*)\s(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i',
' <a href=""'.$attrs.' title="" target="_blank" rel="nofollow"></a>',
$str
);
return $str;
}
谁能帮帮我。
输入:
$cont=autolink("https://www.example.com/test.php?ss=a https://www.example.com/2 https://www.exxxxxxxxxxxxxxxxxxaaample.com/3");
输出电流:
<a href="https://www.example.com/test.php?ss=a" title="https://www.example.com/test.php?ss=a" target="_blank" rel="nofollow">www.example.com</a> <a href="https://www.example.com/2" title="https://www.example.com/2" target="_blank" rel="nofollow">www.example.com</a> <a href="https://www.exxxxxxxxxxxxxxxxxxaaample.com/3" title="https://www.exxxxxxxxxxxxxxxxxxaaample.com/3" target="_blank" rel="nofollow">www.exxxxxxxxxxxxxxxxxxaaample.com</a>
想要的输出:
<a href="https://www.example.com/test.php?ss=a" title="https://www.example.com/test.php?ss=a" target="_blank" rel="nofollow">www.example.com/test...</a> <a href="https://www.example.com/2" title="https://www.example.com/2" target="_blank" rel="nofollow">www.example.com/2</a> <a href="https://www.exxxxxxxxxxxxxxxxxxaaample.com/3" title="https://www.exxxxxxxxxxxxxxxxxxaaample.com/3" target="_blank" rel="nofollow">www.exxxxxxxxxxxxx.....</a>
我应该可以将 URL 名称限制为 40 个字符。无论如何,第三个属性不需要并且会被修改,不返回域,但 URL 的真实名称限制为 40 个字符。
我希望它是最终的:答案是 *preg_replace_callback*
,这是最好的正则表达式,在这里使用,使文本 URL 到 HTML link 的最佳方法,简单修改。
function autolink($textOriginal)
{
$textOriginal=" $textOriginal";
$urlPattern = '|([\w]*)\s(https?://([\w.-]+\.[\w.]{2,6})[^\s\]\[\<\>]*/?)|i';
$textNew = preg_replace_callback($urlPattern, "replaceLinkParts", $textOriginal);
return $textNew;
}
function replaceLinkParts($matches)
{
$thefullurl=''.$matches[2].'';
$whatisthis=''.$matches[1].'';
//////////
$myDomainx = parse_url("$thefullurl");
$simpleurl="" . $myDomainx["host"] . "";
$simpleurlp = "" . $myDomainx["path"] . "";
if ($simpleurlp == "/") {
$simpleurlp = "";
}
$newurl="$simpleurl$simpleurlp";
////////////////
$lastpart= "";
$limichars="30";
$limitlast="10";
$calcit=$limichars-$limitlast;
$nmre=strlen($newurl);
if ($nmre > $limichars) {
$lastpart=$nmre - $limitlast;
$lastpart= substr($newurl, $lastpart, $limitlast);
$newurl= substr($newurl, 0, $calcit);
$newurl= ''.$newurl.'...';
}
////////////
$retthis=''.$whatisthis.' <a target="_blank" title="'.$thefullurl.'" rel="nofollow" href="'.$thefullurl.'">'.$newurl.''.$lastpart.'</a>';
return $retthis;
}
输出HTMLlink:www.example.com...ifverylongurl