substr関数を使用して文字列を出力し、出力を100文字に制限しています。問題は、文字列に100文字を超えるURLが含まれている場合があることです。
テキストリンクを[リンク]のような一般的なものに置き換えることで、100文字の制限内でURLを出力する方法について誰かアドバイスがありますか?
私のコード:
<?php
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
if(preg_match($reg_exUrl, $content, $url)) {
$content = preg_replace($reg_exUrl, "<a href=\"{$url[0]}\" target=\"_blank\">Link</a>", $content);
if (strlen($content) > 100) {
echo substr($content, 0, 100).'...';
}
} else {
if (strlen($content) > 100) { echo substr(stripslashes($content), 0, 100).'...'; } else { echo stripslashes($content); }
}
?>