以下を使用して、$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
}