note 52065 added to function.preg-match-all

From: Date: Wed, 20 Apr 2005 13:25:46 +0000
Subject: note 52065 added to function.preg-match-all
Groups: php.notes 
Request: Send a blank email to php-notes+get-88260@lists.php.net to get a copy of this message
I wrote a function, which takes urls from a string, or database output, highlights them, and shortens the links name if its above 30 characters. Note: You'll have to use nl2br() function on the string before using it, because I didn't know how to check for LineFeed or CarrigeReturn in preg-style. <?php function urlhighlight($str) { preg_match_all("/http:\/\/?[^ ]+/i",$str,$lnk); $size = sizeof($lnk[0]); $i = 0; while ($i < $size) { $len = strlen($lnk[0][$i]); if($len > 30) { $lnk_txt = substr($lnk[0][$i],0,30)."..."; } else { $lnk_txt = $lnk[0][$i]; } $ahref = $lnk[0][$i]; $str = str_replace($ahref,"<a href='$ahref'>$lnk_txt</a>",$str); $i++; } return $str; } ?> Ex: <?php $str = "a lot of text with urls in it and alot of linebreaks"; $str = urlhighlight(nl2br($str)); ?> ---- Manual Page -- http://www.php.net/manual/en/function.preg-match-all.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+52065 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52065&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52065&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52065&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52065&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52065&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+52065&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#88260) next »