PHP 一起使用 str_replace、substr 和 strpos
我想做的是转换 youtube/vimeo 上视频的 url,返回一个可以嵌入到 iframe 中并自动执行视频嵌入代码的新 url。
这就像一个魅力。
$url = "http://vimeo.com/29431603";
$orj_value = array("watch?v=", "http://vimeo.com/");
$new_value = array("embed/", "http://player.vimeo.com/video/");
$url = str_replace($orj_value, $new_value, $url);
echo $url;
你知道,youtube 也会在以 & 开头的 url 中广告一个丑陋的字符串,所以当我
$url = substr($url, 0, strpos($url, "&"));
在它之前
echo $url;
添加它时,它会删除 youtube url 中不需要的部分。但是,vimeo 代码中没有 &里面只是不返回任何东西。似乎添加新行后,代码想要看到一个 &网址内;否则 echo $url var 显示空白屏幕。
2-对于dailymotion,我需要删除第一个_之后的所有内容 我应该如何编辑下面的行才能包含 dailymotion 的支持?
$url = substr($url, 0, strpos($url, "&"));
预先感谢任何愿意回答的人:)
What I am trying to do is to convert url of a video on youtube/vimeo return a new url that can be embed into an iframe and automate video embed codes.
This is working like a charm.
$url = "http://vimeo.com/29431603";
$orj_value = array("watch?v=", "http://vimeo.com/");
$new_value = array("embed/", "http://player.vimeo.com/video/");
$url = str_replace($orj_value, $new_value, $url);
echo $url;
You know, youtube ads an ugly string too into url that starts with &, so when i include
$url = substr($url, 0, strpos($url, "&"));
before
echo $url;
it strips out the unwanted part of youtube url. But then, vimeo code which has no & inside just does not return anything. Seems after adding new line, code wants to see a & inside the url; otherwise to echo $url var displays blank screen.
2- For dailymotion, i need to strip out everything after first _
How should i edit the line below in order to include support of dailymotion too?
$url = substr($url, 0, strpos($url, "&"));
Thanks in advance to anyone going to answer :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 strpos() 找不到您搜索的字符,它将返回 false,这可能会混淆 substr()。
尝试这样的事情:
至于你的评论:
有更优雅的方法可以做到这一点(例如 REGEX),但这可以。请注意 &或者 _ 不能是字符串中的第一个字符,如果您想允许将 !== false 添加到条件中。
If strpos() can't find the character you search for it will return false, which could confuse substr().
Try something like this:
As for your comment:
There are more elegant ways to do it (ex. REGEX) but this will do. Note that & or _ can't be the first character in string if you want to allow this add !== false to the condition.