Regular expressions work well for this type of thing, but often strpos or substr work faster. Check out the PHP documentation for preg_match() . Below examples there is a note for this thing.
Here is the prototype code:
function videoType($url) { if (strpos($url, 'youtube') > 0) { return 'youtube'; } elseif (strpos($url, 'vimeo') > 0) { return 'vimeo'; } else { return 'unknown'; } }
Obviously returning a string is not a good idea, but you get the point. Substitute your own business logic.
As others noted in the comments, this is a quick and dirty solution that does a great job with edge cases. If the url contains "youtube" (example.com/youtube), it will return a false result. The parse_url() solution mentioned below is a much more robust solution.
source share