1

次のように文字列を分割するのに問題があります。テキストのままにするテキストとは、アンカーに変換するリンクと、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 部分を実行しており、アンカーでは何も起こりません。

4

2 に答える 2

2

このパターンを URL に使用します

preg_replace('/([^\'"])((ht|f)tps?:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i',
    '$1<A target="_blank" href="$2">$2</A>',$string);

そして、タグ配置ステートメントのIFRAME前に配置ステートメントをA置きます。

    /*** make all the IFrame links ***/
$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 URLs links ***/
$string = preg_replace('/([^\'"])((ht|f)tps?:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i',
        '$1<A target="_blank" href="$2">$2</A>',$string);

仕組みをご覧ください。

于 2013-01-20T07:04:50.960 に答える
0

正規表現のアプローチを使用できます。

if (preg_match("/http://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/)(\w*)(&(amp;)?[\w\?=]*)?/i", $link))
{
    // It's a Youtube link!
}

ただし、このアプローチも使用できます。

$parsedlink = parse_url($link);

if ($parsedlink['host'] == 'www.youtube.com')
{
    // It's a Youtube link!
}
于 2013-01-20T03:56:58.800 に答える