0

I need create a function that checks a parsed value to see if it matches a few other values and then return that match. For example I am trying to match video urls correctly. So if it's youtube do this or if it's vimeo do this or if it's nothing do this. I know how to create a function but I'm not sure what to use for the parse, would it be parse_url?

For my test cases I need to send in the right parameter and then see that the returned values are matching what I want them to be.

Here's what I've tried so far:

function get_video_embed_string($videostring) {
      $video_url_parse = parse_url( $videostring, PHP_URL_HOST );  //get the input string ready to parse
      $returnstring = "";  //default return string to empty string

      if ($video_url_parse === 'vimeo.com') {
          $returnstring = str_replace( 'vimeo.com', 'player.vimeo.com', $video_url_parse );
      } else if ($video_url_parse === 'youtube.com') {
          $returnstring  = str_replace( 'youtube.com', 'youtube.com/embed/',   $video_url_parse );
      } else { 
          //do nothing 
      } 

      return $returnstring;
}

parse_str($returnstring);

//now setup your test cases and see what echos out of the above method

if ($returnstring === 'player.vimeo.com') {
     echo "vimeo: <" .  get_video_embed_string ("https://vimeo.com/abcdefg123") . ">";
} else if ($returnstring === 'youtube.com/embed/'){
     echo "youtube: <" . get_video_embed_string   ("https://youtube.com/abcdefg123")  . ">";
} else if($returnstring === '' ){
     echo "nothing: <" . get_video_embed_string ("https://abc123.com/abcdefg123") . ">";
} else {
     echo "empty:< " . get_video_embed_string ("") . ">";
}
Erik Terwan
  • 2,710
  • 19
  • 28
lostInTheTetons
  • 1,162
  • 1
  • 13
  • 23

2 Answers2

2

parse_url() is very good for parsing URLs and - in your case - extract the host name from it.

Your example is a little messed up. $returnstring is not defined outside of your function. You should turn error reporting on, so you will see NOTICE messages on this kind of errors.

I assume, your function should return the video embed url, not only the host name. So you should do your replace on $videostring, not $video_url_parse:

function get_video_embed_string($videostring) {
    $video_url_parse = parse_url( $videostring, PHP_URL_HOST );  //get the input string ready to parse
    $returnstring = "";  //default return string to empty string

    if ($video_url_parse === 'vimeo.com') {
        $returnstring = str_replace( 'vimeo.com', 'player.vimeo.com', $videostring );
    } else if ($video_url_parse === 'youtube.com') {
        $returnstring  = str_replace( 'youtube.com', 'youtube.com/embed', $videostring );
    } else {
        //do nothing
    }

    return $returnstring;
}

This will give you this output:

echo get_video_embed_string("https://vimeo.com/abcdefg123"); // https://player.vimeo.com/abcdefg123
echo get_video_embed_string("https://youtube.com/abcdefg123"); // https://youtube.com/embed/abcdefg123
echo get_video_embed_string("https://abc123.com/abcdefg123"); // <empty string>

[For a more robust approach, I would probably try to extract the video ID from all known valid URL schemes using regexp and just insert this ID in the embed url.]

Community
  • 1
  • 1
DerVO
  • 3,679
  • 1
  • 23
  • 27
  • Thank you.. I will actually need to pull the video ID from WP and insert the new ID into the new embed url. So I use regexp to do this? – lostInTheTetons May 31 '16 at 16:16
2

I think you're on the right track using parse_url, but I have a couple suggestions for improvement:

  • instead of the run-on if/elseif chain, use a switch
  • the str_replace isn't working well as is because you're replacing the parsed host, so why spend the overhead searching again for the string to replace when you've already found it.
  • in the user comments for parse_url, there's an excellent example to reconstruct the parsed url. this will avoid string replacements where the host name is also part of the url (www.youtube.com/youtubevideo123)
  • simplify your test cases by just calling your function for each case instead of another if/else chain check.

function get_video_embed_string($videostring) {

  $video_url_parse = parse_url($videostring);  //get the input string ready to parse

  switch ($video_url_parse['host']) {
      case 'vimeo.com':
          $video_url_parse['host'] = 'player.vimeo.com';
          return unparse_url($video_url_parse);
      case 'youtube.com':
          $video_url_parse['host'] = 'youtube.com/embed';
          return unparse_url($video_url_parse);
      default:
          return unparse_url($video_url_parse);
  }

}

function unparse_url($parsed_url) { 
  $scheme   = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : ''; 
  $host     = isset($parsed_url['host']) ? $parsed_url['host'] : ''; 
  $port     = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; 
  $user     = isset($parsed_url['user']) ? $parsed_url['user'] : ''; 
  $pass     = isset($parsed_url['pass']) ? ':' . $parsed_url['pass']  : ''; 
  $pass     = ($user || $pass) ? "$pass@" : ''; 
  $path     = isset($parsed_url['path']) ? $parsed_url['path'] : ''; 
  $query    = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : ''; 
  $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : ''; 
  return "$scheme$user$pass$host$port$path$query$fragment"; 
}


//now setup your test cases and see what echos out of the above method

echo "vimeo: <" .  get_video_embed_string ("https://vimeo.com/abcdefg123") . ">\n";
echo "youtube: <" . get_video_embed_string   ("https://youtube.com/abcdefg123")  . ">\n";
echo "nothing: <" . get_video_embed_string ("https://abc123.com/abcdefg123") . ">\n";
echo "empty:< " . get_video_embed_string ("") . ">\n";

This will result in the following output in source:

vimeo: <https://player.vimeo.com/abcdefg123>
youtube: <https://youtube.com/embed/abcdefg123>
nothing: <https://abc123.com/abcdefg123>
empty:< >
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167