Jump to content

Check if user entered text contains allowed domain values?

Go to solution Solved by QuickOldCar,

Recommended Posts

Code what i made so far.

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

$allowedDomains = 'www.google.com

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

if(preg_match($regex, $inputText)){
    print 'Domain match!';
    print 'Domain not match!';
Edited by jacob21
Link to comment
Share on other sites

  • Solution

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.

//parse url function
function parseTheUrl($url)
    $new_parse_url = str_ireplace(array(
    ), "", 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(
//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) {
    //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;
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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