开发者

PHP preg_replace question?

开发者 https://www.devze.com 2023-02-10 03:56 出处:网络
hey guys, I\'m a preg_replace noob and don\'t understand how to solve the following case: $youtubeurl = \"((http|https)\\:\\/\\/(www|it|co\\.uk|ie|br|pl|jp|fr|es|nl|de)\\.youtube\\.(com|it|co\\.uk|ie

hey guys, I'm a preg_replace noob and don't understand how to solve the following case:

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

I have a pattern that matches any youtube URL. If this pattern is matched i want to call the function embedCode() and pass along the matched string.

How can i do this. right now i'm obviously passing along the regexp code which is of course wrong. I need to pass along the matched string.

thank you


Try preg_replace_callback.


$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]);
}

You just have to find the index of matched url, try print_r($content); inside the if and see what is the index for the matched pattern.


You are trying to do this:

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

精彩评论

暂无评论...
验证码 换一张
取 消