List of URLs allowed with wildcard in visitor input

1

In my form I would like the visitor to be able to enter in a field any URL that is test.com or test2.com (wildcard), for example test.com/loquesea.

I have the following in PHP:

$allowedsites = array(
    'http://test.com/*',
    'http://test2.com/*',
);

if(in_array($longlink, $allowedsites)) {
    $error = "The URL is in the array!";
}else{
    $error = "The URL doesn't exists in the array.";
    include ("crear.php");
    exit;
}

I have tried without * and with *, it has not worked for me.

Thank you.

    
asked by Jose Antonio 04.04.2017 в 23:02
source

1 answer

0

in_array looks for an exact match, if you try to evaluate variations of the string you need regular expressions.

$patron = '/^http:[s]?\/\/test[a-z0-9]*\.com\/[a-z]*$/';
$url = 'http://test.com/';

preg_match($patron, $url);
    
answered by 05.04.2017 в 03:53