次のように文字列を分割するのに問題があります。テキストのままにするテキストとは、アンカーに変換するリンクと、YouTube からのリンクを iframe タグに変換するものです。
私のスクリプトは次のようになります:
$string='This is a link www.dinamomania.net this is a http://www.youtube.com/watch?v=U9LB6qGvdpQ';
echo makelink($string);
function makeLink($string){
/*** make sure there is an http:// on all URLs ***/
$string = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2",$string);
/*** make all URLs links ***/
$string = preg_replace('/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i','<a target="_blank" href="$1">$1</A>',$string);
$string = preg_replace('/((http|ftp)\:\/\/)?([w]{3}\.)?(youtube\.)([a-z]{2,4})(\/watch\?v=)([a-zA-Z0-9_-]+)(\&feature=)?([a-zA-Z0-9_-]+)?/', '<iframe style= "margin:15px 0 15px 0;display:block;" width="500" height="300" src="http://www.youtube.com/embed/$7" frameborder="0" allowfullscreen></iframe>',$string);
/*** make all emails hot links ***/
$string = preg_replace("/([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}[0-9]{1,3})(\]?))/i","<A HREF=\"mailto:$1\">$1</A>",$string);
return $string;
}
}
iframe の前にこの "> のようなアンカーを取得します。これは、最初にアンカーへのリンクを実行し、次に YouTube 形式の iframe を実行しているためです。つまり、アンカーへのインフレームです。
私はこのようなことをするif文を探していました:
if( is link from youtube ) {
// do the iframe part
} else {
// do the anchor part
}
ヘルプ/アドバイスをいただければ幸いです。ありがとう !
私はこのようなものを持ってきました:
if (preg_match("/((http|ftp)\:\/\/)?([w]{3}\.)?(youtube\.)([a-z]{2,4})(\/watch\?v=)([a-zA-Z0-9_-]+)(\&feature=)?([a-zA-Z0-9_-]+)?/", $string))
{
$string = preg_replace('/((http|ftp)\:\/\/)?([w]{3}\.)?(youtube\.)([a-z]{2,4})(\/watch\?v=)([a-zA-Z0-9_-]+)(\&feature=)?([a-zA-Z0-9_-]+)?/', '<iframe style= "margin:15px 0 15px 0;display:block;" width="500" height="300" src="http://www.youtube.com/embed/$7" frameborder="0" allowfullscreen></iframe>',$string);
} else {
$string = preg_replace('/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i','<a target="_blank" href="$1">$1</A>',$string);
}
しかし、それは私の iframe 部分を実行しており、アンカーでは何も起こりません。