note 52065 deleted from function.preg-match-all by colder

From: Date: Mon, 16 Apr 2007 16:57:22 +0000
Subject: note 52065 deleted from function.preg-match-all by colder
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-125459@lists.php.net to get a copy of this message
Note Submitter: Dan Madsen ---- 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)); ?>

« previous php.notes (#125459) next »