scotty123 Posted July 15, 2008 Share Posted July 15, 2008 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. Hi Scott, i have found a realy cool map, check it out: http://maps.google.co.uk/maps?utm_campaign=en&utm_source=en-ha-emea-uk-goog-gm&utm_medium=ha&utm_term=google%20map bye bye 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 Quote Link to comment https://forums.phpfreaks.com/topic/114917-url-truncating/ Share on other sites More sharing options...
MadTechie Posted July 15, 2008 Share Posted July 15, 2008 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)."..."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/114917-url-truncating/#findComment-591047 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.