Jump to content

URL Truncating


scotty123

Recommended Posts

Hi Guys, here is my code so far:

 

$str="http://maps.google.co.uk/maps?utm_campaign=en&utm_source=en-ha-emea-uk-goog-gm&utm_medium=ha&utm_term=google%20map";
echo "<p>String is: $str</p>";

function dot($str, $len=40, $dots = "...") {
    if (strlen($str) > $len) {
        $dotlen = strlen($dots);
        $str2 = substr_replace($str, $dots, $len - $dotlen);
        $str = "<a href='$str' title='$str'>$str2</a>";
    }
    return $str;
}

echo dot($str);

 

This passes a long url to the function, it truncates it if necessary and displays my url.

 

The bit i am struggling with is the original string, it won't just contain a url but also normal text.  For example.

 

 

What i need to do is pass the above text to a regular expression, extract a url if it is found and pass it to my function which truncates it  ???

 

My new string would look like

Hi Scott, i have found a realy cool map, check it out:

http://maps.google.co.uk/maps?utm_cam...

bye bye

 

please help  ;)

 

Link to comment
https://forums.phpfreaks.com/topic/114917-url-truncating/
Share on other sites

this was written on the fly but should work.

<?php
$str="http://maps.google.co.uk/maps?utm_campaign=en&utm_source=en-ha-emea-uk-goog-gm&utm_medium=ha&utm_term=google%20map";

$newstr = preg_replace_callback("/(https?:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]){0}/si","dot",$str);
echo "<p>String is: $newstr</p>";

function dot($url)
{
$len=40;
return substr($url, $len)."...";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/114917-url-truncating/#findComment-591047
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.