Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.