2011-02-14 73 views
1

嗨, 我是小白的preg_replace,不知道如何解決以下情況:PHP preg_replace問題?

$youtubeurl = "((http|https)\:\/\/(www|it|co\.uk|ie|br|pl|jp|fr|es|nl|de)\.youtube\.(com|it|co\.uk|ie|br|pl|jp|fr|es|nl|de)([a-zA-Z0-9\-\.\/\?_=&;]*))"; 
$content = preg_replace($youtubeurl, embedCode($youtubeurl), $content); 

我有匹配任何YouTube網址模式。 如果這個模式匹配,我想調用函數embedCode()並傳遞匹配的字符串。

我該怎麼做。現在我明顯傳遞了正則表達式代碼,這當然是錯誤的。我需要傳遞匹配的字符串。

謝謝

回答

0
$youtube_url_pattern = "#((http|https)\:\/\/(www|it|co\.uk|ie|br|pl|jp|fr|es|nl|de)\.youtube\.(com|it|co\.uk|ie|br|pl|jp|fr|es|nl|de)([a-zA-Z0-9\-\.\/\?_=&;]*))#"; 

if(preg_match($youtube_url_pattern, $content_containing_url, $content)){ 
    embedCore($contet[index]); 
} 

你只需要找到匹配的URL的指標,儘量print_r($content);if裏面,看看什麼是匹配模式的索引。

0

您正在試圖做到這一點:

if (preg_match('/' . $youtubeurl . '/', $content, $match)) 
{ 
    embedCode($match); 
}