Log in

View Full Version : preg_match Help with adding apostrophe and other special chars



JasonDFR
10-29-2008, 08:44 AM
if ( preg_match("/^[ÀÈÌÒÙàèìòùÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûçÇA-Za-z0-9]{2,40}$/", $_POST['theme_name']) ) {
$theme_name = $_POST['theme_name'];
} else { // Theme name NOT ok
$errors[] = '<p class="error">Blah Blah</p>';
}

The above code works, I think.... But I would like to allow these characters as well "? - ' : ! ."

I can't seem to get anything to work or perhaps I am not testing it correctly.

The apostrophe is giving me trouble in particular. And it seems that the apostrophe is escaped automaticly when POSTed by the form. For example
echo $_POST['theme_name']; will output "cat\'s" when "cat's" is entered in the input field.

Thanks a lot!

J

james438
10-29-2008, 09:56 AM
This is only half an answer, but
<?php
$kk="thisisgreat.-:?'!";

if ( preg_match("/^[?:\-\.'!ÀÈÌÒÙàèìòùÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûçÇA-Za-z0-9]{2,40}$/", $kk) ) {
echo "looks good. $kk";
} else {
echo "does not look so good. $kk";
}
?> works well enough for me.

I think I know what the problem is with the $_POST part of your code, but I am gonna put that off till I get a few ZZs. For now it is probably easier to do a simple str_replace for your single and double quotes just before it is displayed. $_POST manipulates quotes automatically for some reason, but it has been so long that I need to look up what I did about that.

james438
10-29-2008, 07:31 PM
I wouldn't worry about the single quote problem. $_POST does this so that the content can be entered into the database correctly.

If you want to display the content of a $_POST variable without the slashes I would use


echo stripslashes($_POST['theme_name']);

Just remember that when you pull the info from the database it will look fine. The slashes won't be there.