PHP String Manipulation - Grab video URL to place into a video Object

Hello all,

I need to grab a string like this:

"Hello, please have a look to my new video on youtube, it’s very nice: http://www.youtube.com/… it’s so cool. And you know what? I have also this one here: www.vimeo.com/… also c00l. :slight_smile: "

The user commonly could omit the http:// part of the url.

I’ve been given this solution:


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

//parse the grabbed URL:
$urlData = parse_url($url);

switch(strtolower($urlData['host']))
{
    case "youtube.com":
        // the youtube video object sintax goes here.
    break;
    case "vimeo.com":
        // the vimeo video object sintax goes here.
    break;
    case "something.tld":
        // the other video object sintax goes here.
    break;
}

before the actually object syntax, I need to grab the query string related, on the case of youtube will be v, so, something like this:


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

My difficulties are as follow:

  1. Some strings, may have more then one video URL inside. If that’s the case, I need to grab them both. - It seems that this doesn’t accommodate that possibility.

  2. On the prepare part, it seems to work ok if we have only a string with the URL, however on the example, we can well see that that’s not the case. We may well have text before and after the url.

Can I have your help in order to overcome those difficulties.
For 2) should I use some sort of regex ?
What about the 1) where should that loop be placed?

Little lost here,
Márcio

Just notice that this post doesn’t belong here, but into the php section. :frowning: Sorry. If a moderator can remove it… :smiley:

[fphp]parse_url[/fphp]

Well… :slight_smile:

I have this array with 3 main keys.
“images”, “downloads” and “videos”.

Here’s a dump example:

    
array(3) {
    ["images"]=>
      array(1) {
        [0]=>
        array(3) {
          ["cod_teamFk"]=>
          string(2) "51"
          ["attachement"]=>
          string(22) "210111011642_51_57.jpg"
          ["bonus"]=>
          string(1) "0"
        }
    }
    ["downloads"]=>
      array(1) {
        [0]=>
        array(3) {
          ["cod_teamFk"]=>
          string(2) "51"
          ["attachement"]=>
          string(22) "210111011732_51_23.zip"
          ["bonus"]=>
          string(1) "0"
        }
    }
    ["videos"]=>
      array(1) {
        [0]=>
        array(1) {
          ["youtube"]=>
          array(1) {
            [0]=>
            string(150) "object video"
          }
        }
    }
}

In order to produce this I’ve used those methods here:

 
public  function SplitProof($proof) {
  $arr = array();
  $arr["images"] = array();
  $arr["downloads"] = array();
  $arr["videos"] = array();
  $arrDownloads = array("zip"=>1, "rar"=>1);
  $arrImage = array("jpg"=>1,"jpeg"=>1);
  
  if (count($proof))
  {
    foreach ($proof as $r)
    { 
      $file = "./lib/teams/".$r["attachement"];
      if (is_file($file))
      {
         $pathinfo = pathinfo($file);
         $ext = strtolower($pathinfo["extension"]);
         
         if ($arrImage[$ext] == 1)
         {
           $fileThumb = "./lib/teams/thumb".$r["attachement"];
            
           if (!file_exists($fileThumb))
           {
             $thumb = new Thumbnail("lib/teams/".$r["attachement"]);
             $thumb->cropFromCenterNoSquare(230, 153);
             $thumb->show(100,$fileThumb);
           }
            
           $arr["images"][] = $r;
         }
            
         if ($arrDownloads[$ext] == 1)
         {
           $arr["downloads"][] = $r;
         } 
                                 
       }
                            
        //ISSUE HERE - DON'T KNOW HOW TO PROPERLY 
       //ADD THOSE VIDEOS HERE
       if ($this->_splitVideo($r['proof']) != false)
       {
         $arr["videos"][] = $this->_splitVideo($r['proof']);
       }
      } //eo foreach
            		
    }//eo if
            
    return $arr;
   }

and

 
/**
 * @desc Finds a video reference a convert it into a video object, finally,
 * store in into an array.
 * 
 * @param <string> $proof
 * @return string
 */
 private function _splitVideo($proof) {
  $video_links = array();
    if (preg_match_all("'(http://)?(www\\.)?(youtube|vimeo|videos\\.frog)\\.([a-z0-9_/?&+=.]+)'is",$proof,$n))
    {
      foreach ($n[3] as $key => $site)
      { 
        $urlCorreto[$key] = str_replace(array("http://www.","http://", "www."),"",$n[0][$key]);
        $urlCorreto[$key] = "http://" . $urlCorreto[$key];
            
        $data = parse_url($urlCorreto[$key]);
            
        switch(strtolower($data['host']))
        {
          case "youtube.com":  
            parse_str($data["query"],$result);
            $videoId = $result["v"];
            $objetoYoutube = '<iframe title="YouTube video player" width="380" height="225" src="http://www.youtube.com/embed/'.$videoId.'" frameborder="0" allowfullscreen></iframe>';
            
             $video_links[$site][] = $objetoYoutube;
            
           break;
            
           case "vimeo.com": 
             $vimeoPath = $data["path"]; 
             $objetoVimeo = '<iframe src="http://player.vimeo.com/video'.$vimeoPath.'" width="380" height="225" frameborder="0"></iframe>';
            
             $video_links[$site][] = $objetoVimeo;
           break;
            
                                    
           case "videos.frog.es":
            $frogPath = $data["path"];
            $objetofrog = '<embed src="http://rd3.videos.frog.es/play?file=http://rd3.videos.frog.es'.$frogPath.'/mov/1" type="application/x-shockwave-flash" allowFullScreen="true" width="380" height="225"></embed>';
            
             $video_links[$site][] = $objetofrog;
            
           break;
       }
      }
      return $video_links;
    } 
    else { 
       return false;
    }            
}

As you can see, “images” and “downloads” are associated with proof records $r.
Videos however, aren’t.

I would like to request your help in order to allow videos key to be, integrated with $r, just like images and downloads are, so that, instead of the video dump that I have now, I could have something like:

    
["videos"]=>
      array(1) {
        [0]=>
        array(3) {
          ["cod_teamFk"]=>
          string(2) "51"
          ["bonus"]=>
          string(1) "0"
          ["attachement"]=>
          NULL
          ["videoObject"]=> 
          string(17) "video object here"
        }
    }

Can you help me out achieving such task?

Thanks a lot in advance

Change:


    if ($this->_splitVideo($r['proof']) != false)
    {
      $arr["videos"][] = $this->_splitVideo($r['proof']);
    }

to:

    if ($this->_splitVideo($r['proof']) != false)
    {
      $r["videoObject"] = $this->_splitVideo($r['proof']);
      $arr["videos"][] = $r;
    }

Now it’s just a question of adapting to splitVideo method to output only the video objects, instead of an array.

:slight_smile:

Yupi!!!