I am writing a function to parse some videosites urls in order to generate embedding html:
if (strstr($url, 'a.com')) {
$from = 'a';
} elseif (strstr($url, 'b.com')) {
$from = 'b';
} else {
return 'Wrong Video Url!';
}
if ($from == 'a') {
// use preg_match() to retrieve video id to generate embedding html
if (preg_match('#^http://a\.com/id_(\w*?)\.html$#', $url, $matches)) {
// return video embedding html
}
return 'Wrong a.com Video Url!';
}
if ($from == 'b') {
if (preg_match('#^http://b\.com/v_(\w*?)\.html$#', $url, $matches)) {
//return video embedding html
}
return 'Wrong b.com Video Url!';
}
My purpose of using strstr()
is reducing calls of preg_match()
in some situations, for example if I have b.com
urls like this: http://www.b.com/v_OTQ2MDE4MDg.html
, I don't have to call preg_match()
twice.
But I am still not sure if this kind of practice is good or if there is a better way.