1

ok i'm trying to shorten a url but keep the actual link intact.

I'm using this code at the moment, can't find a simple working solution...

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

example of what i want to achieve.

input http://www.example.com/839283ur9283ru2938u2389ru23irj3.html

output http://www.example.com/839283u...

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

i need it to automatically find url links in a description and make them active? while cutting them down to a given length.

ty

4

1 回答 1

2

try with parse_url() and 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>';
于 2012-09-02T23:45:10.510 回答