2

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 :)

Mehmet Yaden
  • 277
  • 3
  • 9

1 Answers1

1

If strpos() can't find the character you search for it will return false, which could confuse substr().

Try something like this:

$url = strpos($url, "&") ? substr($url, 0, strpos($url, "&")) : $url;

As for your comment:

if(strpos($url, "&")) {
    $url = substr($url, 0, strpos($url, "&"));
} else if(strpos($url, "_")) {
    $url = substr($url, 0, strpos($url, "_"));
}

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.

Hikaru-Shindo
  • 1,891
  • 12
  • 22