Validating Name Input that contains apostrophes and hyphens

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
BHarris
Forum Newbie
Posts: 7
Joined: Thu Jun 09, 2005 3:46 pm
Location: West Virginia
Contact:

Validating Name Input that contains apostrophes and hyphens

Post by BHarris »

I am fairly new to php and using the functions correctly to validate certain information. I know that this is fairly simple to do but when I print out my data which is taken from a database names such as O'Hara comes out with 7 slashes in front of the apostrophe. The name is stored in the database (mysql) as is "O'Hara". I basically need an idea so I may see what is the best way to to apply in trying to fix that problem. I presently have this little thing that I am using, however, it is not working correctly.

Code: Select all

<?php
function check_name(&$name)
	{
	      $name = trim($name);
		   if(((eregi('^[A-Za-z]+[\'\-]?',$name) === FALSE)))
			return FALSE;
				return TRUE;


	}
?>
My Result is something like this:

Code: Select all

O\\\\\\\'Hara, Joe
Well, I hope I can get this little problem fixed. Thanks
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

stripslashes() is your friend :D
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

erhm.. first, that's a lot of (). second, preg_match is faster. third, your regex is really goofed.

Code: Select all

function check_name(&$name) {
  $name = trim($name);
  if (!preg_match('/^[a-z\'\-]+$/i',$name)) return false;
  return true;
}
but yeah, stripslashes will get that other problem fixed. ;)
BHarris
Forum Newbie
Posts: 7
Joined: Thu Jun 09, 2005 3:46 pm
Location: West Virginia
Contact:

Follow up to my name validation

Post by BHarris »

Thanks for the info you provided. However, when I try this new function using the "preg_match" function the way that you have suggested, I am unable to enter names such as "O'Hara" (it goes to my error message that points out that the name is invalid). What do you think needs to be changed in order to accept and name such as "O'Hara"?
BHarris
Forum Newbie
Posts: 7
Joined: Thu Jun 09, 2005 3:46 pm
Location: West Virginia
Contact:

Storing data into the database without slashes

Post by BHarris »

Stripslashes(), works very well to get rid of the slashes when viewing the data that had been slashed. However, for some reason these slashes are still on when when I insert this data into the database as part of my "insert_event()" function that was written. Does anyone know why these slashes (as many as 15) would be added to the data when it is being sent to the database even though it does not have the slashes when viewed just before being sent to the database? If anyone knows, what is the best way to get rid of that problem?

Thanks.
Post Reply