checking for url and HTML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kapil1089theking
Forum Commoner
Posts: 46
Joined: Wed May 28, 2008 1:51 pm
Location: Kolkata, India
Contact:

checking for url and HTML

Post by kapil1089theking »

How can I check out weather $POST['textfield'] has URL or HTML Code,

code will assign false if the string from textField contains URL or HTML else true.

So need to check out the presence of URL or HTML Code.
User avatar
dbsuk
Forum Newbie
Posts: 7
Joined: Thu Dec 17, 2009 9:22 am
Location: London, England

Re: checking for url and HTML

Post by dbsuk »

Hi again,

You would need to use a regular expression (http://www.regular-expressions.info/php.html).

The following function would do what you need.

Try....

Code: Select all

 
<?
 
       function ContainsHtml ($var) {
        
        if(ereg("(\]+)\>)", $var)) { 
        
            return true; 
            
        } else { 
        
            return false;
            
        }
        
    }
 
       // you would then use it like this.
 
      if(ContainsHtml($_POST['textarea'])) {
 
             // warn the user that html is not allowed
 
         echo 'Html is not allowed in the textarea';
      
      } else {
 
             // No html tags present, insert the data into the database.
         
      }
 
?>
 
Regards
Richard
Last edited by dbsuk on Thu Dec 17, 2009 8:14 pm, edited 1 time in total.
kapil1089theking
Forum Commoner
Posts: 46
Joined: Wed May 28, 2008 1:51 pm
Location: Kolkata, India
Contact:

Re: checking for url and HTML

Post by kapil1089theking »

how to check URL?

thanx for you lovely support again.
User avatar
dbsuk
Forum Newbie
Posts: 7
Joined: Thu Dec 17, 2009 9:22 am
Location: London, England

Re: checking for url and HTML

Post by dbsuk »

I only read part of the question first time round.

Could not really find anything solid for detecting URLs with regular expressions, you need regexp guru.

However this would work:

Code: Select all

 
<?
 
    function ContainsUrl($var) {
 
     // array containing a list of filters, add more to this list
     // I only thought of a few, you could add all TLD's if you like.
    $filters = array('http://',
                         'www.',
                         '.com',
                         '.co.uk',
                         '.biz',
                         '.info',
                         '.name',
                         '.net',
                         '.org',
                         '.pro',
                         '.org'
                         );
        // Cycle through the array of filters 
        foreach($filters as $filter) {
            
            // check if the $var contains any of the filters.
            if(strpos($var,$filter)!==false) {
            
                return true;
            
            }   
            
        }
 
        // if none of the filters are found in $var, return false.
        return false;
 
    }
    
 
    // To use it 
 
     if(ContainsUrl($_POST['textarea'])) {
 
        echo 'URLS are not allowed'; 
      
      } else {
 
         // insert the data into the database.
         
         }
 
?>
 
To use both functions together you could do:

Code: Select all

 
<?
    
    // Use the || Logical Operator - meaning: OR
    if(ContainsUrl($_POST['textarea']) || ContainsHtml($_POST['textarea'])) {
 
        echo 'URLS and HTML tags are not allowed'; 
      
      } else {
 
         // insert the data into the database.
         
         }
 
?>
Hopefully your well on your way now.

Best regards

Richard
kapil1089theking
Forum Commoner
Posts: 46
Joined: Wed May 28, 2008 1:51 pm
Location: Kolkata, India
Contact:

Re: checking for url and HTML

Post by kapil1089theking »

How to check blanks
I mean It will check wether any character other than blank is present in the text field or not just to ensure no blank string is entered into the table.
kapil1089theking
Forum Commoner
Posts: 46
Joined: Wed May 28, 2008 1:51 pm
Location: Kolkata, India
Contact:

Re: checking for url and HTML

Post by kapil1089theking »

thanx... Have used trim function...
Tons of thanx for ur help and support.
Post Reply