Search with regular expressions

Asked

Viewed 32 times

2

Hello I would like your help with the following problem, which I have in this code.

    function verificarParametro($loopurl,$urlHost) {
if (preg_match("/(\W|^){$urlHost}(\W|$)/i", $loopurl))
    return true;
    else
    return false;

It checks the url by taking the parameter put in $urlHost and checking in $loopurl , if the true exists, otherwise the false.

Now here is my problem, I need you to do a complete check from start to end of the parameter that was specified in the field $urlHost, even if it has any special characters or any other type of parameter, for example /*+-!|?\: etc..

For example, if I put to search this parameter below, will give line error.

if (verificarParametroA('https://drive.google.com/file/d/abcdfghiglmnopqrstuvxz/
', 'drive.google.com/file/d/')) {

    echo 'Sim'; }
  • 2

    Use preg_quote($urlHost,'/') to escape the URL. But I think you’re using ER for something you don’t need, a php strpos() would solve!

  • Note that the use of strpos and strips will also fail if you have something like this verificarParametroA('https://foo.com?u=http://bar.com', 'bar.com'), suggest the parse_url: https://answall.com/a/305297/3635 - since the use of strpos with (bool) there in the other answer is totally mistaken, since the return 0 would cause false, when in fact 0 means that it found from the first character in strpos.

2 answers

1

If $loopurl needs to be contained exactly the same within $urlHost can be used strpos. Example:

function verificarParametro($loopurl, $urlHost)
{
    return (bool) strpos('https://drive.google.com/file/d/abcdfghiglmnopqrstuvxz/', $loopurl);
}
  • 1

    Would not be return strpos(...) !== false; ?

  • Hm, truth forgot that it needs a boolean value. I edited the answer.

  • Thank you, you found the value perfectly.

1


Passing the parameter directly into the expression will cause problems:

"/(\W|^){$urlHost}(\W|$)/i"

You must escape the characters with preg_quote (as quoted by @W.Faustino with delimiter /), something else is that the regex should probably be:

/^(\W|){$urlHost}(\W|$)/i

For if the signal ^ is not at the beginning a url so could return TRUE:

verificarParametroA('https://foo.com?u=http://bar.com', 'bar.com')

Note that the use of strpos and stripos will also fail

It should look like this (it really isn’t necessary an if and Else in this case):

function verificarParametro($loopurl, $urlHost) {
    $urlHost = preg_quote($urlHost, '/');
    return !!preg_match("/^(\W|){$urlHost}(\W|$)/i", $loopurl);
}

However I really recommend using the function parse_url, it will extract the necessary data and so you will be able to make a more accurate check so:

function verificarParametro($loopurl, $urlHost)
{
    $host = parse_url($loopurl, PHP_URL_HOST);
    return strcasecmp($host, $urlHost) == 0;
}

Note: used strcasecmp to compare whether both strings are equal independently in case-insenstive

Browser other questions tagged

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