1

以下を使用して、$content内のすべてのURLを検索します

 $content = preg_match_all( '/(http[s]?:[^\s]*)/i', $content, $links );

ただし、これはのhttp://部分によって異なりますhttp://www.google.com/some/path

私の質問は:

1-で始まるリンクもヒットするように変更するにはどうすればよいwwwですwww.google.comか?

2-主な目的は、リンクを見つけて、それらを別の関数から返される値に置き換えることです。preg_match_callback()を試しましたが、機能していません(おそらく間違って使用しています..

$content = preg_replace_callback(
           "/(http[s]?:[^\s]*)/i",
            "my_callback",
            $content);

function my_callback(){

// do a lot of stuff independently of preg_replace
// adding to =.output...

return $output;
}

さて、私のロジックでは(おそらく間違っています)、からのすべての一致は。$contentに置き換えられ$outputます。私は何を間違っているのですか?

(匿名関数は使用しないでください-私は古いサーバーでテストしています)

編集I-コメントの後、より詳細に明確にしようとしています

function o99_simple_parse($content){

$content = preg_replace_callback( '/(http[s]?:[^\s]*)/i', 'o99_simple_callback', $content );


return $content;
}

折り返し電話 :

function o99_simple_callback($url){
    // how to get the URL which is actually the match? and width ??
        $url = esc_url_raw( $link );
        $url_name = parse_url($url); 
        $url_name = $description = $url_name['host'];// get rid of http://..
        $url = 'http://something' .  urlencode($url)   . '?w=' . $width ; 
        return $url; // what i really need to replace 
    }
4

1 に答える 1

3

で始まるURLを許可する必要がある正規表現を変更するにはwww、次のように記述します。

/((http[s]?:|www[.])[^\s]*)/i
  +         ++++++++
于 2013-03-27T02:54:44.857 に答える