1

I have this string that may contain some urls that I need to grab. For instance, if the user does:

www.youtube ...

or

www.vimeo ...

or

http://www.youtube ...

or

HttP://WwW.viMeo

I need to grab it (until he finds a space perhaps). and store it on a already created array.

The need is to separate the vimeo links from the youtube ones and place each of those on the appropriate video object.

I'm not sure if this is possible, I mean, if the URL coming from the browser could be used to be placed on a predefined video object. If it is, then this is the way to go (so I believe).

If all this is feasible, can I have your help in order to build such a rule?

Thanks in advance

2 Answers 2

5

This matches the links you need, and store them in a 2D array by site name:

$video_links = array();
if (preg_match_all("'(http://)?(www[.])?(youtube|vimeo)[^\s]+'is",$str,$n)) {
    foreach ($n[3] as $key => $site)
    {
        $video_links[$site][] = $n[0][$key];
    }
}

What does this do?

This match separates 3 + 1 parts of the needed urls in $str, which is your string:

  • Part 0: the whole match (your video link)
  • Part 1: http:// (optional)
  • Part 2: www. (optional)
  • Part 3: vimeo or youtube

preg_match_all returns a 2D array with the above part numbers at first level, and every match inside is the part of each match. So you iterate part 3 of the match ($n[3]), and use the array keys to reference part 0 ($n[0][$key]), and arrange them in a nice 2D array like this:

$video_links = array (
    'vimeo' => array (
        0 => 'vimeo link 1',
        1 => 'vimeo link 2',
        // ...
    ),
    'youtube' => array (
        0 => 'youtube link 1',
        1 => 'youtube link 2',
        // ...
    )
);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I have several questions, wondering if you could give me a hand here. Where do we use $str ? What is the meaning of 3 ? why do we need $site ... I will read about preg_match_all right way but those questions still apply - can I have your help once again? Thanks a lot.
Added some explanations, hope you find your answers in it :)
thanks a lot! :) (I will properly study it) - One last question about the regex expression: why [^\s]+'is" ?
[^\s]+ in the end means "all the following non-whitespace characters, but at least one". The is modifiers after the delimiter are: i - case-insensitive, s - multiline (this is not really nessesary here, but I'm really used to using it)
1

What you should do is first replace all instance of http:// and www. with nothing, and then prepend it back on to the string, this makes the string consistent

str_replace(array("http://www.","http://"),"",$url);
$url = "http://" . $url;

then you can use parse_url to check the data like so

$Data = parse_url($url);

Then just check your values accordingly.

switch(strtolower($Data['host']))
{
    case "youtube.com":
        // :)
    break;
    case "vimeo.com":
        // :)
    break;
    case "something.tld":
        // :)
    break;
}

The dump of $Data would output something like so:

[scheme] => http
[host] => youtube.com
[user] => 
[pass] => 
[path] => /watch
[query] => v=r8FVAHuQvjc&feature=topvideos
[fragment] =>

you can now just go

$lastSegment = $Data["path"] . "?" . $Data["query"];

which would return something like /watch?v=r8FVAHuQvjc&feature=topvideos

if you wanted individual items from the query such as the video id you can then go:

parse_str($Data["query"],$result);
echo $result["v"];

which would just output the video id.

6 Comments

@RobertPitt: Thanks a lot - your approach seems to be very clean indeed. Where, on your example, do we find the end of the string?
@RobertPitt : Thanks a lot. I will have a look on it later tonight. It seems really a very nice method, preferable over the other method, if we need more detailed information and manipulation over our string. (taking that we(me) don't get much about regex). Cheers.
@RobertPitt - I have a question. I do have a string with something like: ""hello, please have a look on my vimeo video here: www.vimeo.com/... " This string is stored on a array key like: "$details['videoDetails']" - I believe your example doesn't accommodate this, or am I wrong ?
I notice that $url will be, on this case, $detais['videoDetails] - still, NOT all strings will have an URL inside so, something like: $url = "http://" . $url; will produce strange results no ? I need to do this, only if, it finds a URL or, am I wrong? K. Regards again.
Your correct in thinking that this does not find the urls, out of a string, this takes an pre-found url and allows you to validate the type of url and its components such as video id etc, you would be best to use a regex to retrieve the the link from the content, and then run it threw this kind of checks to validate the link.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.