参考:PHP » autolink() – Seebz Code
<?php function autolink($str, $attributes=array()) { $attrs = ''; foreach ($attributes as $attribute => $value) { $attrs .= " {$attribute}=\"{$value}\""; } $str = ' ' . $str; $str = preg_replace( '`([^"=\'>])((http|https|ftp)://[^\s<]+[^\s<\.)])`i', '$1<a href="$2"'.$attrs.'>$2</a>', $str ); $str = substr($str, 1); return $str; } ?>
EX1
<?php $str = 'A link : http://example.com/?param=value#anchor.'; $str = autolink($str); echo $str; // A link : <a href="http://example.com/?param=value#anchor">http://example.com/?param=value#anchor</a>. ?>
EX2
<?php $str = 'http://example.com/'; $str = autolink($str, array("target"=>"_blank","rel"=>"nofollow")); echo $str; // <a href="http://example.com/" target="_blank" rel="nofollow">http://example.com/</a> ?>
日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)