Jump to content

Scanning general text - user inputs - for bad coding.


iarp

Recommended Posts

My current function for checking over user inputs is below, email and phone numbers work just fine.

 

I'm more worried about the insufficiency of text and textarea, this isn't being used live anywhere atm while i remake it and i'm out of ideas on how to scan general data.

 

Fields passing through text, textarea might be full names, addresses, subjects basically general things. I've been stumbling on ideas of what would be best practice to do this.

 

Any ideas?

 

# $tbc = data to be cleaned
# $type = email, phone, text, textarea
function escape_data($tbc, $type='text') {
	switch($type) {
		case 'email':
			if(preg_match('/(\w+\.)*\w+@(\w+\.)*\w+(\w+\-\w+)*\.\w+/', $tbc)){
				$op = $tbc;
			} else {
				$op = false;
			}
			break;
		case 'phone':
			if (!empty($tbc)) { 
				preg_match_all('/[0-9\(\)+.\- ]/s', $tbc, $cleaned); 
				foreach($cleaned[0] as $k=>$v) { 
					$ready .= $v; 
				}
				if ((strlen($ready) > 10) && (strlen($ready) <=25)) {
					$op = $ready;
				} else {
					$op = false;
				}
			 } else {
				$op = false;
			 }
			break;
		case 'text':
		case 'textarea':
			if (!empty($tbc)) { 
				$op = strip_tags($tbc);
			} else {
				$op = false;
			}
			break;
		default:
			$op = false;
	}
	return $op;
}

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.

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