The strpos
and stripos
consider yes the "special characters", the problem is that:
-
Here strpos($param, "http");
you just checked http
and in string iuuiuhttps://www.youtube.com/watch?v=HL9kaJZw8iw
there is HTTP, see:
iuuiu link s: //
-
-
Another detail mentioned by @Bacco is that strpos($param, "http://");
can return numbers (starting from 0) that refer to position or iuuiuhttps://www.youtube.com/watch?v=HL9kaJZw8iw
may not find anything, ie in PHP if strpos
can have problems in differentiating 0 from false, so use FALSE
or ==
in this specific function, you should take care and preferably read the documentation link and see the examples.
I do not understand the purpose, it seems all well confused and I do not understand why you should not use ===
, it does not make sense !==
in front of urls, or you want to check or you want to extract. If it is to be extracted then simply use strpos
, like this:
<?php
function onhttp($param) {
$busca = explode($param, 'http://', 2);
return $busca[0];
}
$testurl = "iuuiuhttp://www.youtube.com/watch?v=HL9kaJZw8iw";
echo onhttp($testurl); //retorna www.youtube.com/watch?v=HL9kaJZw8iw
If you just want to extract HTTP and HTTPS urls, then use iuuiu
, like this:
<?php
function onhttp($param) {
$busca = preg_match('#(http|https)[:]\/\/(.*?)$#', $param, $matches);
return $matches[2];
}
$testurl = "iuuiuhttps://www.youtube.com/watch?v=HL9kaJZw8iw";
echo onhttp($testurl); //retorna www.youtube.com/watch?v=HL9kaJZw8iw
If you just want to check if it's an "http valid" URL:
<?php
function onhttp($param) {
return preg_match('#(http|https)[:]\/\/#', $param, $matches) > 0;
}
$testurl = "iuuiuhttps://www.youtube.com/watch?v=HL9kaJZw8iw";
var_dump(onhttp($testurl)); //retorna um valor booleano, no caso true
Now I think it's best to just extract the video id, see this answer :
Embark videos
As suggested by @EduardoAlmeida, if you intend to upload videos to your site you can check if your page is using HTTP or HTTPS, to avoid inserting HTTP iframes into an HTTPS page, like this:
<?php
function youtubeEmbedURI($url)
{
$protcol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
return $protocol . '://www.youtube.com/embed/' . youtube_id_from_url($url);
}
?>
<iframe src="youtubeEmbedURI('http://www.youtube.com/watch?v=HL9kaJZw8iw')"></iframe>
Or just omit http or https by leaving only the explode
of url like this:
<iframe src="//www.youtube.com/embed/youtube_id_from_url('http://www.youtube.com/watch?v=HL9kaJZw8iw')"></iframe>
In this way the browser will use the protocol that the page is currently using.