jacob21 Posted October 16, 2014 Share Posted October 16, 2014 (edited) 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!'; } Edited October 16, 2014 by jacob21 Quote Link to comment https://forums.phpfreaks.com/topic/291861-check-if-user-entered-text-contains-allowed-domain-values/ Share on other sites More sharing options...
ginerjm Posted October 16, 2014 Share Posted October 16, 2014 I would set up the allowed names in an array and then just simply use the in_array function. Quote Link to comment https://forums.phpfreaks.com/topic/291861-check-if-user-entered-text-contains-allowed-domain-values/#findComment-1493889 Share on other sites More sharing options...
jacob21 Posted October 16, 2014 Author Share Posted October 16, 2014 I would set up the allowed names in an array and then just simply use the in_array function. It works only when input text contains url Quote Link to comment https://forums.phpfreaks.com/topic/291861-check-if-user-entered-text-contains-allowed-domain-values/#findComment-1493904 Share on other sites More sharing options...
ginerjm Posted October 16, 2014 Share Posted October 16, 2014 Huh? Quote Link to comment https://forums.phpfreaks.com/topic/291861-check-if-user-entered-text-contains-allowed-domain-values/#findComment-1493924 Share on other sites More sharing options...
Solution QuickOldCar Posted October 16, 2014 Solution Share Posted October 16, 2014 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/291861-check-if-user-entered-text-contains-allowed-domain-values/#findComment-1493936 Share on other sites More sharing options...
jacob21 Posted October 17, 2014 Author Share Posted October 17, 2014 Thanks QuickOldCar for this code Quote Link to comment https://forums.phpfreaks.com/topic/291861-check-if-user-entered-text-contains-allowed-domain-values/#findComment-1494021 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.