The strpos
and stripos
consider rather "special characters", the problem is that :
Here strpos($param, "http");
you just checked the http
and in string iuuiuhttps://www.youtube.com/watch?v=HL9kaJZw8iw
exists HTTP, see:
iuuiuhttps://
Already here strpos($param, "http://");
you want to check if you have http:// but the url passed is https, it will never find iuuiuhttps://www.youtube.com/watch?v=HL9kaJZw8iw
.
Another detail quoted by @Bacco is that strpos
can return numbers (starting from 0) that refer to position or FALSE
chance does not find anything, ie in PHP if using ==
may have problems differentiating 0 from false, so use ===
or !==
in this specific function, you should be careful and preferably read the documentation http://php.net/manual/en/function.strpos.php and see the examples.
I do not understand the goal, everything seems very confusing and also do not understand why should not use strpos
, has no sense the iuuiu
in front of the urls, either you want to check or you want to extract. if extracting then simply use explode
, thus:
<?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 only want to extract HTTP and HTTPS urls then use preg_match
, thus:
<?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 is an "http validate 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 response:
Embark videos
At @Eduardoalmeida’s suggestion, if you intend to ship the videos on your site you can check if your page is using HTTP or HTTPS, to avoid inserting HTTP iframes on 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 simply omit http or https leaving only the //
of the url like this:
<iframe src="//www.youtube.com/embed/youtube_id_from_url('http://www.youtube.com/watch?v=HL9kaJZw8iw')"></iframe>
This way the browser will use the protocol that the page is currently using.
That is not true: "It does not search in string for a common character together with a special character." - and to know if the beginning is
https://
just see if thestrpos( $param, 'https://' ) === 0
(note the three=
)– Bacco
And if you want to find in any position:
strpos( $param, 'https://' ) !== false
– Bacco
Wow, that’s a real lack of attention from me. And this is not the first time I can’t do something by not paying attention between http and https thank you very much.
– ayelsew