CakePHPでミニブログをつくる・その4
入力テキストにURLが含まれていたら自動でリンクする
app/config/bootstrap.php に独自関数を記述。
//オートリンク
function autoLink($string){
$text = htmlspecialchars($string, ENT_QUOTES);
$text = preg_replace('/(https?:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:@&=+$,%#]+)/', '<a href="$1">$1</a>', $text);
return $text;
}
app/views/posts/index.thtml
<span class="message"><?php echo autoLink($post['Post']['text']); ?></span>
