PHP preg_replace 问题?
嘿伙计们, 我是 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 URL 匹配的模式。 如果此模式匹配,我想调用函数 embedCode() 并传递匹配的字符串。
我怎样才能做到这一点。现在我显然正在传递正则表达式代码,这当然是错误的。我需要传递匹配的字符串。
谢谢
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|pl|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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试 preg_replace_callback。
Try preg_replace_callback.
您只需找到匹配网址的索引,尝试
if
内的print_r($content);
并查看匹配模式的索引是什么。You just have to find the index of matched url, try
print_r($content);
inside theif
and see what is the index for the matched pattern.你正在尝试这样做:
You are trying to do this: