使用HREF链接缩短URL

时间:2012-09-02 23:39:54

标签: php url

好吧我正在尝试缩短网址但保持实际链接不变。

我现在正在使用此代码,找不到简单易用的解决方案......

$description = preg_replace('/https?:\/\/[^\s<]+/i', '<a href="\0">\0</a>', $description);

我想要实现的例子。

输入http://www.example.com/839283ur9283ru2938u2389ru23irj3.html

输出http://www.example.com/839283u ...

<a href="http://www.example.com/839283ur9283ru2938u2389ru23irj3.html">http://www.example.com/839283u...</a>

我需要它来自动查找说明中的网址链接并使其处于活动状态?同时将它们削减到给定的长度。

TY

1 个答案:

答案 0 :(得分:2)

尝试使用parse_url()substr()

$url = parse_url('http://www.example.com/839283ur9283ru2938u2389ru23irj3.html');
$newUrl = $url['scheme'].'://'.$url['host'].substr($url['path'], 0, 8);
echo '<a href="'.$url.'">'.$newUrl.'...</a>';
相关问题