npsari Posted July 17, 2007 Share Posted July 17, 2007 Hi there I am using this code to shrink URL to a desired number of characters function clickable_short_url ($matches) { ### Entire match. $url = array_shift($matches); ### Only http:// match, if any. $http = array_shift($matches); ### Shorten textual part if need be. $url_text = strlen($url) >= 60 ? (substr($url, 0, 20) . '...' . substr($url, -10, 10)) : $url ; ### Add http:// prefix if need be. if (! $http) { $url = 'http://' . $url; } ### Return new url. return '<a target="_blank" href="' . $url . '">' . $url_text . '</a>'; } $pattern = '% (http://)? ### optional http:// prefix (?(1)|www\.) ### require www. if there is no http:// \S+ ### gobble anything but white space %x'; echo nl2br(preg_replace_callback($pattern, 'clickable_short_url', $row['Description'])); But i dont want only the URL to be shrinked, in fact, i want any text to be shrinked Because when someone types hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii (without space), it damages the look of my table So, how can i make the code shrink all words which are longer that 25 letters to something like hiiiiiiiii.....iiii Quote Link to comment Share on other sites More sharing options...
MadTechie Posted July 17, 2007 Share Posted July 17, 2007 what about this <?php $theString = "123456789012345789012345678901234578901234567890123457890"; $max = 25; $start = 17; $end = 5; $sLength = strlen($theString); if($sLength > $max) { $NewString = substr($theString, 0, $start); $NewString .= "..."; $NewString .= substr($theString, ($sLength-$end), $end ); }else{ $NewString = $theString; } echo $NewString; ?> output 12345678901234578...57890 EDIT or change 60 to 25 ### Shorten textual part if need be. $url_text = strlen($url) >= 25 ? (substr($url, 0, 20) . '...' . substr($url, -10, 10)) : $url ; Quote Link to comment Share on other sites More sharing options...
npsari Posted July 17, 2007 Author Share Posted July 17, 2007 thats a great way of doing it the only problem is that your code does not identify the long words by itself because, i cant make a long work equal to the string, because long words are in a bunch of text any idea how this can be fixed Quote Link to comment Share on other sites More sharing options...
MadTechie Posted July 17, 2007 Share Posted July 17, 2007 maybe this <?php $theString = "1234567 8901234578901234567890123457890123456 7890123457890"; $max = 25; $start = 17; $end = 5; $words = explode(" ",$theString); foreach($words as $word) { $sLength = strlen($word); if($sLength > $max) { $NewString = substr($word, 0, $start); $NewString .= "..."; $NewString .= substr($word, ($sLength-$end), $end ); }else{ $NewString = $word; } echo $NewString." "; } ?> 1234567 89012345789012345...23456 7890123457890 Quote Link to comment Share on other sites More sharing options...
npsari Posted July 17, 2007 Author Share Posted July 17, 2007 thats amazing pal ohh, i discovered a little hustle when the words are many, like hi, my name is blaha blah, i likeeeeeeeeeeeeeeeeeeee to play footbal, adios, byeeee the code shows one word u know what might be the problem Quote Link to comment Share on other sites More sharing options...
MadTechie Posted July 17, 2007 Share Posted July 17, 2007 Seams OK here, Note the changes! <?php //New String $theString = "hi, my name is blaha blah, i likeeeeeeeeeeeeeeeeeeee to play footbal, adios, byeeee"; $max = 10; //changed from 25 to 10 $start = 6;//changed from 17 to 6 $end = 3; //changed from 5 to 3 $words = explode(" ",$theString); foreach($words as $word) { $sLength = strlen($word); if($sLength > $max) { $NewString = substr($word, 0, $start); $NewString .= "..."; $NewString .= substr($word, ($sLength-$end), $end ); }else{ $NewString = $word; } echo $NewString." "; } ?> output hi, my name is blaha blah, i likeee...eee to play footbal, adios, byeeee Quote Link to comment 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.