1

画像リンク、YouTube ビデオ リンク、および通常のリンクを適切な html タグに個別に置き換えようとしていますが、通常のリンクだけをターゲットにするのに問題があります。コードは次のとおりです。

 function($text)
  {
    $output = preg_replace('#(http://([^\s]*)\.(jpg|gif|png))#', 
                        '<br/><img src="$1" alt="" width="300px" style = "clear:both;"/><br/>', $text);

        $output = preg_replace('#(http://([^\s]*)youtube\.com/watch\?v=([^\s]*))#', 
                      '<br/><iframe width="480px" height="385px" src="http://www.youtube.com/embed/$3"
                       frameborder="0" allowfullscreen style = "clear:both;"></iframe><br/>', $output);

    $output = preg_replace('#(http://([^\s]*)\.(com))#', 
                      '<br/><a href="$1">$1</a><br/>', $output);

    return $output

 }

これは代わりに、最後のステップですべてのリンクを置き換えています...どうすればこれを回避し、最後のステップでリンク (YouTube または画像ではない) のみを置き換えることができますか?

ありがとう!

4

2 に答える 2

1

探している URL は、スペースや改行などで区切る必要があります。区切り文字を正規表現に追加するだけで、最後の正規表現が貪欲になりすぎません! 例えば...

<?php

$text = "
http://www.youtube.com/watch?v=yQ4TPIfCK9A&feature=autoplay&list=FLBIwq18tUFrujiPd3HLPaGw&playnext=1\n
http://www.asdf.com/asdf.png\n
http://www.asdf.com\n
";

var_export($text);
var_export(replace($text));

function replace($text)
{
    $output = preg_replace('#(http://([^\s]*)\.(jpg|gif|png))\n#', 
                        '<br/><img src="$1" alt="" width="300px" style = "clear:both;"/><br/>', $text);


    $output = preg_replace('#(http://([^\s]*)youtube\.com/watch\?v=([^\s]*))\n#', 
                      '<br/><iframe width="480px" height="385px" src="http://www.youtube.com/embed/$3"
                       frameborder="0" allowfullscreen style = "clear:both;"></iframe><br/>', $output);

    $output = preg_replace('#(http://([^\s]*)\.(com))\n#', 
                      '<br/><a href="$1">$1</a><br/>', $output);

    return $output;
}
于 2012-09-17T05:42:09.667 に答える
0

preg_replace_callback各リンクを一度だけ照合するために使用できます。したがって、リンクを 2 回変更することを心配する必要はありません。

$input = <<<EOM
http://www.example.com/fritzli.jpeg

https://www.youtube.com/watch?v=blabla+bla+"+bla

http://wwww.google.com/search?q=blabla
EOM;
echo replace($input);

function replace($text) {
    $output = preg_replace_callback("#(https?://[^\s]+)#", function($umatch) {
        $url = htmlspecialchars($umatch[1]);

        if(preg_match("#\.(jpg|jpeg|gif|png|bmp)$#i", $url)) {
            $result = "<br/><img src=\"$url\" alt=\"\" width=\"300px\" "
                . " style = \"clear:both;\"/><br/>";
        } elseif(preg_match("#youtube\.com/watch\?v=([^\s]+)#", $url, $match)) {
            $result = "<br/><iframe width=\"480px\" height=\"385px\""
                . " src=\"http://www.youtube.com/embed/{$match[1]}\""
                . " frameborder=\"0\" allowfullscreen style = \"clear:both;\">"
                . "</iframe><br/>";
        } else {
            $result = "<br/><a href=\"$url\">$url</a><br/>";
        }

        return $result;
    }, $text);

    return $output;
}

注: 上記のコードは、PHP バージョン >= 5.3 でのみ機能します。5.3 以下を使用する場合は、内部関数を別の関数に抽出し、関数名を引数として に指定できますpreg_replace_callback

function replace_callback($umatch) {
    $url = htmlspecialchars($umatch[1]);

    if(preg_match("#\.(jpg|jpeg|gif|png|bmp)$#i", $url)) {
        $result = "<br/><img src=\"$url\" alt=\"\" width=\"300px\" "
            . " style = \"clear:both;\"/><br/>";
    } elseif(preg_match("#youtube\.com/watch\?v=([^\s]+)#", $url, $match)) {
        $result = "<br/><iframe width=\"480px\" height=\"385px\""
            . " src=\"http://www.youtube.com/embed/{$match[1]}\""
            . " frameborder=\"0\" allowfullscreen style = \"clear:both;\">"
            . "</iframe><br/>";
    } else {
        $result = "<br/><a href=\"$url\">$url</a><br/>";
    }

    return $result;
}

function replace($text) {
    $output = preg_replace_callback("#(https?://[^\s]+)#",
        "replace_callback", // the name of the inner function goes here
        $text);

    return $output;
}
于 2012-09-17T05:50:32.107 に答える