Jump to content

Check if user entered text contains allowed domain values?


jacob21

Recommended Posts

Code what i made so far.

$inputText = 'This is testing http://www.youtube.com';

$allowedDomains = 'www.google.com
		   youtube.com/
		   http://www.test.org';

$array = preg_split('/[\s]+/', $allowedDomains);
$regex = '';//Need this line

if(preg_match($regex, $inputText)){
    print 'Domain match!';
}else{
    print 'Domain not match!';
}

This will check exact domains only, so if wanted the subdomains as well need to add them into $allowedDomains

 

Since this is text and people do not always add a www. or a protocol like http:// or https://, I searched for decimal points instead.

<?php
//parse url function
function parseTheUrl($url)
{
    $new_parse_url = str_ireplace(array(
        "www.",
        "mms://",
        "rtsp://",
        "http://",
        "https://",
        "http://",
        "ftp://",
        "feed://"
    ), "", trim($url));
    $parsedUrl     = @parse_url("http://$new_parse_url");
    return strtolower(trim($parsedUrl['host'] ? $parsedUrl['host'] : array_shift(explode('/', $parsedUrl['path'], 2))));
}

//input text examples
$inputText = "come visit my http://viagara.com website or come to pills.com";
//$inputText = "A normal message with no urls in it.";
//$inputText = "This is testing http://www.youtube.com and also .google.com..... lets see what 5.27 does";


//alowed domains array
$allowedDomains = array(
    "google.com",
    "youtube.com",
    "test.org"
);
//remove multiple whitespace and trim
$str            = preg_replace('~\s+~', ' ', trim($inputText));
//explode all words by spaces
$array          = explode(" ", $str);
//loop all the words
foreach ($array as $value) {
    //replace multiple decimals with a single
    $value = preg_replace('~\.+~', '.', trim($value));
    //remove decimal from beginning
    $value = ltrim($value, ".");
    //remove decimal from end
    $value = rtrim($value, ".");
    //check for any with a decimal within, most likely a url
    if (preg_match('~\.~', $value)) {
        //exclude numbers at end of expected domain
        if (ctype_alpha(end(explode(".", $value)))) {
            //create array with domains found in message
            $domains[] = parseTheUrl($value); //parsing them for better results and lowercases
        }
    }
}


//checking exact domain

//if $domain array exists
if ($domains) {
    //print_r($domains);
    //loop domains
    foreach ($domains as $domain) {
        //check if is not in allowedDomains
        if (!in_array($domain, $allowedDomains)) {
            $inputText = "Message not allowed"; //change inputText
        }
    }
}

//echo the message
echo $inputText;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.