PHP preg_替换问题?
嘿,伙计们, 我是一名孕妇,不知道如何解决以下情况:PHP preg_替换问题?,php,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), $con
$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 URL的模式。
如果这个模式匹配,我想调用函数embedCode并传递匹配的字符串
我怎样才能做到这一点。现在我显然在传递regexp代码,这当然是错误的。我需要传递匹配的字符串
多谢各位
$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的索引,然后尝试打印\r$content;在if中,查看匹配模式的索引是什么。您正在尝试这样做:
if ( preg_match( '/' . $youtubeurl . '/', $content, $match ) )
{
embedCode( $match );
}
试试。这是如何工作的?那么,我应该传递什么函数?@mathiregister:阅读文档。回调函数获取一个匹配数组,因此要获取完整的URL,必须再次组合这些部分。例如,在回调中,您可以执行以下操作:返回EmbeddeCodeImplode,$matches;。虽然这在理论上可以回答这个问题,但在这里包括答案的基本部分,并提供链接供参考。