查找自定义标签之间的字符串并用它做一些事情
在我的新项目中,成员可以使用我的 YouTube API,它将生成一个如下所示的标签,以便他们在他们想要的任何文本区域中使用它
<ymedia>http://www.youtube-nocookie.com/v/qQG0XfU-bFs</ymedia>
: 并使用 $ymstring
为从 ymedia 标记收集的字符串生成类似的代码:
<?php
$u_agent = $_SERVER[''HTTP_USER_AGENT''];
if(preg_match(''/Opera/i'',$u_agent)){
<object width="320" height="240" data="".$ymstring."?fs=1&hl=en_US&rel=0"><param name="movie" value="".$ymstring."?fs=1&hl=en_US&rel=0" /><param name="allowFullScreen" value="true" /><param name="wmode" value="transparent" /><param name="bgColor" value="#ffffff" /><param name="flashvars" value="vu=".$ymstring."?fs=1&hl=en_US&rel=0" /></object>
} else {
<object width="320" height="240"><param name="movie" value="".$ymstring."?fs=1&hl=en_US&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="".$ymstring."?fs=1&hl=en_US&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="320" height="240"></embed></object>
}
?>
基本上,我试图在 $string 中查找所有 ymedia 标记并将它们替换为我上面针对 Opera 或其他其他浏览器的 YouTube 编码。
On my new project, members are able to use my YouTube API and it will generate a tag like below for them to use it in whatever textarea they want:
<ymedia>http://www.youtube-nocookie.com/v/qQG0XfU-bFs</ymedia>
I want my PHP profile page to take the string between <ymedia></ymedia>
and generate a similar code below using $ymstring
for the string collected from the ymedia tag:
<?php
$u_agent = $_SERVER[''HTTP_USER_AGENT''];
if(preg_match(''/Opera/i'',$u_agent)){
<object width="320" height="240" data="".$ymstring."?fs=1&hl=en_US&rel=0"><param name="movie" value="".$ymstring."?fs=1&hl=en_US&rel=0" /><param name="allowFullScreen" value="true" /><param name="wmode" value="transparent" /><param name="bgColor" value="#ffffff" /><param name="flashvars" value="vu=".$ymstring."?fs=1&hl=en_US&rel=0" /></object>
} else {
<object width="320" height="240"><param name="movie" value="".$ymstring."?fs=1&hl=en_US&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="".$ymstring."?fs=1&hl=en_US&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="320" height="240"></embed></object>
}
?>
Basically, I'm trying to find all the ymedia tags in a $string and replace them with my youtube coding above aimed at either Opera or the rest of other browsers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该有效:
preg_match("/(.*?)<\/ymedia>/", $Input, $Matches);
然后,您的结果将位于
$Matches[1]
中,您可以将其传递为$ymstring
。This should work:
preg_match("/<ymedia>(.*?)<\/ymedia>/", $Input, $Matches);
Then, your result would be in
$Matches[1]
which you could pass as$ymstring
.为了简单起见,您希望使用 preg_replace_callback:
显然您将使用 if/else 构造来返回适当且完整的对象标记。
You want to use preg_replace_callback for simplicity:
Obviously you would use your if/else construct to return the appropriate and complete object tag.