Does anyone know of a function that considers special characters like ": //" in php without being strpos ()? [closed]

0

Does anyone know of a function that considers special characters like ": //" in php without being strpos ()? It does not look in the string for a common character next to a special character. Ex:

    <?php
    function onhttp($param) {
    $busca = strpos($param, "http");
    return $busca;
    }
    $testurl = "iuuiuhttps://www.youtube.com/watch?v=HL9kaJZw8iw";
    echo onhttp($testurl); //Saída: está após 5 caracteres 

    <?php
    function onhttp($param) {
    $busca = strpos($param, "http://");
    return $busca;
    }
    $testurl = "iuuiuhttps://www.youtube.com/watch?v=HL9kaJZw8iw";
    echo onhttp($testurl); //Saída: Nada é retornado.

I want to create a function that checks if the beginning of the url contains the string "http: //".

    
asked by anonymous 26.05.2016 / 21:34

2 answers

5

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.

    
26.05.2016 / 21:54
2

easy

$url = 'http://site.com';

if(substr($url, 0, 7) == 'http://')
    echo "A url comessa com 'http://'";
else
    echo "A url nao comessa com 'http://'";
    
26.05.2016 / 21:57