の使用について少し混乱していますpreg_replace_callback()
内に$content
いくつかの URL を含む があります。
以前私が使用した
$content = preg_match_all( '/(http[s]?:[^\s]*)/i', $content, $links );
foreach ($links[1] as $link ) {
// we have the link. find image , download, replace the content with image
// echo '</br>LINK : '. $link;
$url = esc_url_raw( $link );
$url_name = parse_url($url);
$url_name = $description = $url_name['host'];// get rid of http://..
$url = 'http://somescriptonsite/v1/' . urlencode($url) . '?w=' . $width ;
}
return $url;
しかし、本当に必要なのは、元の URL を解析した URL に置き換えることです...
だから私はpreg_replace_callbackを試しました:
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
}
$url
私は、コールバックは、すべての一致がコールバックを呼び出して (再帰的に?) 結果を取得するように動作し、その結果、$content 内の URL を解析された fromにオンザフライで置き換えることができると想定しましたo99_simple_callbac()
。
しかし、ここでの別の質問(特にこのコメント) が私の疑問を引き起こしました。
実際に一致の配列全体を渡す場合、以前に使用したもの (最初の例) とコールバックの例のpreg_replace_callback()
実際の違いは何ですか?preg_match_all()
私は何が欠けている/誤解していますか?? $content
で見つかった URLを解析された URL に置き換える正しい方法は何でしょうか?