#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Carriage return problem in PHP verify


    I have the following code and want to accept numbers, letters, period, and carriage return. However the following gives error on carriage return.

    $string_exp = "^[a-zA-Z0-9 .|\r]+$";
    if(!eregi($string_exp,$home_address)) {
    $error_message .= 'The Home Address you entered does not appear to be valid.<br />';

    What am I doing wrong with my Regular Expression?

    Lawrence
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,245
    Rep Power
    9400
    You do know that pressing Enter on a Windows system creates a \r\n, right?

    Also: ereg sucks and is going away. Stop using it.
    PHP Code:
    $string_exp '/^[a-zA-Z0-9 .\n\r]$/';
    if (!
    preg_match($string_exp$home_address)) {
        
    $error_message .= "The Home Address you entered does not appear to be valid.<br />"
    Also consider accepting '#' (for apartment numbers, if you don't ask for them separately) and commas (like those between city and state, if you don't ask for those separately).
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0
    Originally Posted by requinix
    You do know that pressing Enter on a Windows system creates a \r\n, right?

    Also: ereg sucks and is going away. Stop using it.
    PHP Code:
    $string_exp '/^[a-zA-Z0-9 .\n\r]$/';
    if (!
    preg_match($string_exp$home_address)) {
        
    $error_message .= "The Home Address you entered does not appear to be valid.<br />"
    Also consider accepting '#' (for apartment numbers, if you don't ask for them separately) and commas (like those between city and state, if you don't ask for those separately).
    Thank you. I am completely new to this and did not know about the /n.

    Being new, I am not aware what ereg is. Where do I learn about this and what do I use instead?

    Thanks for the tips on commas and "#". I tested with my address, and I could not get by the carriage return on the first line.

    Lawrence
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,245
    Rep Power
    9400
    Re: newlines
    Pressing Enter/Return produces:
    - A \r on a Mac
    - A \n on Unix/et al.
    - A \r\n on Windows


    Re: ereg versus preg_match
    ereg is a POSIX-compliant regular expression engine. That's just the proper name for one style of regular expression - there are many. Alone there's nothing wrong with it, but:

    1. PHP also has support for PCRE: Perl-compatible regular expressions. This style is much more powerful than POSIX and, in PHP, often faster. Being a different style and more complex than POSIX it's harder to learn, but for simple expressions the two versions are nearly identical. As you can see, the only difference between the expression you posted and the one I made (besides the missing pipe and added \n) is the added slashes at either end of the string. They don't contribute to the pattern itself but allow for some fancy, PCRE-only features (of which I didn't use any).

    2. Having support for PCRE, PHP will be getting rid of the ereg extension in PHP 6 (along with a number of other "features"). No more ereg functions.

    Comments on this post

    • ManiacDan agrees
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0
    Originally Posted by requinix
    Re: newlines
    Pressing Enter/Return produces:
    - A \r on a Mac
    - A \n on Unix/et al.
    - A \r\n on Windows


    Re: ereg versus preg_match
    ereg is a POSIX-compliant regular expression engine. That's just the proper name for one style of regular expression - there are many. Alone there's nothing wrong with it, but:

    1. PHP also has support for PCRE: Perl-compatible regular expressions. This style is much more powerful than POSIX and, in PHP, often faster. Being a different style and more complex than POSIX it's harder to learn, but for simple expressions the two versions are nearly identical. As you can see, the only difference between the expression you posted and the one I made (besides the missing pipe and added \n) is the added slashes at either end of the string. They don't contribute to the pattern itself but allow for some fancy, PCRE-only features (of which I didn't use any).

    2. Having support for PCRE, PHP will be getting rid of the ereg extension in PHP 6 (along with a number of other "features"). No more ereg functions.
    Again, thank you.

    Lawrence

IMN logo majestic logo threadwatch logo seochat tools logo