Yves Posted June 14, 2008 Share Posted June 14, 2008 Hello. Below you see a function making hyperlinks out of url's in a text string. It works perfectly. However I would like to see the anchortext cut (truncated is the right term I believe) to display 40 characters. <?php function hyperlink ($string) { $string = preg_replace('#(^|\s)([a-z]+://([^\s\w/]?[\w/])*)#is', '\\1<a href="\\2" target="_blank" rel="nofollow">\\2</a>', $string); $string = preg_replace('#(^|\s)((www|ftp)\.([^\s\w/]?[\w/])*)#is', '\\1<a href="http://\\2" target="_blank" rel="nofollow">\\2</a>', $string); return $string; } ?> Example display this: <a href="http://www.domain.com/folder/the-pagetitle-goes-right-here.html">http://www.domain.com/folder/title-goes-...</a> isntead of this: <a href="http://www.domain.com/folder/title-goes-right-here.html">http://www.domain.com/folder/title-goes-right-here.html</a> http://www.domain.com/folder/title-goes- is exactly 40 characters. Notice the "..." at the end of the anchortext too. I hope somebody is kind enough to alter the above function for me. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/110195-solved-function-hyperlink-truncate-anchortext/ Share on other sites More sharing options...
.josh Posted June 14, 2008 Share Posted June 14, 2008 example: <?php $string="abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"; $string = substr($string,0,40) . '...'; echo $string; ?> Quote Link to comment https://forums.phpfreaks.com/topic/110195-solved-function-hyperlink-truncate-anchortext/#findComment-565517 Share on other sites More sharing options...
Yves Posted June 14, 2008 Author Share Posted June 14, 2008 I don't see how I can incorporate that into the function hyperlink () Also, the string doesn't need to be truncate. Just then "\\2" (the anchortext) Quote Link to comment https://forums.phpfreaks.com/topic/110195-solved-function-hyperlink-truncate-anchortext/#findComment-565530 Share on other sites More sharing options...
.josh Posted June 14, 2008 Share Posted June 14, 2008 Your function returns a string. Your script (some other piece that you didn't post) makes it into a link. You want to show only 40 chars of that link string, with a ... at the end of it. So you take the string that's returned from your function, display it in the href = '...', use substr() to show the short version of it between the anchor tags. I assume that this is what your code looks like (the principle, i'm not a mind reader): $string = hyperlink($somethingtoparse); echo "<a href = '$string'>$string</a>"; so change it to this: $string = hyperlink($somethingtoparse); echo "<a href = '$string'>". substr($string,0,40) . "...</a>"; Quote Link to comment https://forums.phpfreaks.com/topic/110195-solved-function-hyperlink-truncate-anchortext/#findComment-565537 Share on other sites More sharing options...
Yves Posted June 14, 2008 Author Share Posted June 14, 2008 Ah. I see what you mean, but... The string is actually a piece of text. example: "This is an example sentence. This is my test site: http://www.mysite.com Go check it out." this is the string that's going through the function. It leaves text as is and changes http links into hyperlinks. So, I can't use your above suggestion in this case. The function will always check a large text block for urls and make those urls clickable. What I want is when the function finds the urls, it has make the urls active but also truncate the longer-than-40-characters urls. Quote Link to comment https://forums.phpfreaks.com/topic/110195-solved-function-hyperlink-truncate-anchortext/#findComment-565663 Share on other sites More sharing options...
thebadbad Posted June 15, 2008 Share Posted June 15, 2008 To tamper with the replacements, you can use preg_match_callback(). Have a look at this: <?php function evaluate1($matches) { return $matches[1] . '<a href="' . $matches[2] . '" target="_blank" rel="nofollow">' . substr($matches[2], 0, 40) . '...</a>'; } function evaluate2($matches) { return $matches[1] . '<a href="http://' . $matches[2] . '" target="_blank" rel="nofollow">' . substr($matches[2], 0, 40) . '...</a>'; } function hyperlink($string) { $string = preg_replace_callback( '#(^|\s)([a-z]+://([^\s\w/]?[\w/])*)#is', 'evaluate1', $string ); $string = preg_replace_callback( '#(^|\s)((www|ftp)\.([^\s\w/]?[\w/])*)#is', 'evaluate2', $string ); return $string; } $string = 'This is an example sentence. This is my test site: http://www.domain.com/folder/the-pagetitle-goes-right-here.html Go check it out.'; echo hyperlink($string); ?> The matches are sent to the specified functions (evaluate1 and evaluate2) as an array of parameters, and these functions are then evaluated. I made them return the strings you're looking for, with the truncated anchor text. $matches[1] and $matches[2] are similar to the back references \\1 and \\2 you were using. Quote Link to comment https://forums.phpfreaks.com/topic/110195-solved-function-hyperlink-truncate-anchortext/#findComment-565703 Share on other sites More sharing options...
Yves Posted June 15, 2008 Author Share Posted June 15, 2008 That's exactly what I wanted. I did alter it slightly, but it's nothing compared to what you came up with! <?php function evaluate1($matches) { if (strlen($matches[2]) > 40 ) { return $matches[1] . '<a href="' . $matches[2] . '" target="_blank" rel="nofollow">' . substr($matches[2], 0, 40) . '...</a>'; } else { return $matches[1] . '<a href="' . $matches[2] . '" target="_blank" rel="nofollow">' . $matches[2] . '</a>'; } } function evaluate2($matches) { if (strlen($matches[2]) > 40 ) { return $matches[1] . '<a href="http://' . $matches[2] . '" target="_blank" rel="nofollow">' . substr($matches[2], 0, 40) . '...</a>'; } else { return $matches[1] . '<a href="http://' . $matches[2] . '" target="_blank" rel="nofollow">' . $matches[2] . '</a>'; } } function hyperlink($string) { $string = preg_replace_callback( '#(^|\s)([a-z]+://([^\s\w/]?[\w/])*)#is', 'evaluate1', $string ); $string = preg_replace_callback( '#(^|\s)((www|ftp)\.([^\s\w/]?[\w/])*)#is', 'evaluate2', $string ); return $string; } ?> Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/110195-solved-function-hyperlink-truncate-anchortext/#findComment-565918 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.