Results 1 to 3 of 3

Thread: preg_match Help with adding apostrophe and other special chars

  1. #1
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default preg_match Help with adding apostrophe and other special chars

    Code:
    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

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    This is only half an answer, but
    PHP Code:
    <?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.

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    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

    Code:
    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.
    Last edited by james438; 10-30-2008 at 06:45 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •