文字列のURLをチェックし、<ahref>タグを追加してリンクを作成する小さなコードがあります。また、YouTubeリンクの文字列をチェックしてから、<a>タグにrel="youtube"を追加します。
YouTubeリンクにrelを追加するだけのコードを取得するにはどうすればよいですか?
どうすれば、任意のタイプの画像リンクに別のrelを追加できますか?
$text = "http://site.com a site www.anothersite.com/ http://www.youtube.com/watch?v=UyxqmghxS6M here is another site";
$linkstring = preg_replace( '/(http|ftp)+(s)?:(\/\/)((\w|\.)+)(\/)?(\S+)?/i', '<a href="\0">\4</a>', $text );
if(preg_match('/http:\/\/www\.youtube\.com\/watch\?v=[^&]+/', $linkstring, $vresult)) {
$linkstring = preg_replace( '/(http|ftp)+(s)?:(\/\/)((\w|\.)+)(\/)?(\S+)?/i', '<a rel="youtube" href="\0">\4</a>', $text );
$type= 'youtube';
}
else {
$type = 'none';
}
echo $text;
echo $linkstring, "<br />";
echo $type, "<br />";