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

Asked

Viewed 140 times

0

Does anyone know of a function that considers special characters like "://" in php without being strpos()? It does not look for a common character together with 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 counts the string "http://".

  • 3

    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 the strpos( $param, 'https://' ) === 0 (note the three = )

  • 1

    And if you want to find in any position: strpos( $param, 'https://' ) !== false

  • 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.

2 answers

5


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.

  • 1

    Beautiful answer! Hey, Guilherme! Can you comment on the use of $_SERVER['HTTPS'] to check if a server, for example, Youtube is using SSL? Thank you!

  • 1

    Perfect, bro!!! Ta the answer to the OP question. Thanks!!!

  • 1

    The part you cut is not correct, it is very confusing. The problem is that the way it did, the return will be false or zero if the string is at the beginning, because the === 0 to find out if it exists and is in the beginning, or !== false to know if you are in any place.

  • 1

    Thank you very much William, and thank you for the prevention in the use of (!== and ==) instead of (== and !=) in this situation.

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://'";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.