note 37349 added to function.preg-replace-callback

From: Date: Tue, 11 Nov 2003 20:47:40 +0000
Subject: note 37349 added to function.preg-replace-callback
Groups: php.notes 
Request: Send a blank email to php-notes+get-60261@lists.php.net to get a copy of this message
You can use this function to display url address as a link or image based on suffix of the url. Every string beginning with www or http(s):// will be displayed as an URL address. But if the string ends with *.jpg or *.gif, it will be displayed as an image. Please note: This function also verifies the size of the image. If is it more than 600x400, it changes it. <? $text = preg_replace_callback($pattern_html,'Check_if_Image',$text); . . function Check_if_Image($matches) { $suffix = strtolower(substr($matches[0],-4)); if ( $suffix == '.jpg' or $suffix == '.gif') { $dsn = 'http'.$matches[3].'://'.$matches[4].$matches[5]; if (list($width, $height, $type, $attr) = getimagesize("$dsn")) { if ( ($width > 600) ) { $koef = $width / 600; $width = 600; $height /= $koef; } if ( ($height > 400) ) { $koef = $height / 400; $height = 400; $width /= $koef; } } else { $height=400; $width=600; } $ret = "<img src=\"$dsn\""; $ret .= "\" border=0 width=\"$width\" height=\"$height\">"; } else { $ret = '<a href="http'.$matches[3].'://'.$matches[4].$matches[5]; $ret .='" target="_blank">'.$matches[0].'</a>'; } return ("$ret"); } ?> ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace-callback.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+37349 Delete -- http://master.php.net/manage/user-notes.php?action=delete+37349&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+37349&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#60261) next »