Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Dec 2006
    Location
    GB
    Posts
    79
    Thanks
    3
    Thanked 1 Time in 1 Post

    A little bit of regex

    I have an input that is only allowed to contain alphanumeric characters and spaces, but no symbols.

    My current regex is shown below, but does always return true if there is atleast one alphanumeric present at the beginning.

    PHP Code:
    preg_match("#^[A-Za-z0-9\s]#"$input
    Any ideas on how this can be achieved?

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Rather than use a RegExp, try the ctype library.

    Since you only want alphanumeric characters and spaces... I'd strip out all spaces first. At that point, you should only be left with alphanumeric characters if the original string was formatted properly. You can then use ctype_alnum() to check this.

    Give this a shot:
    PHP Code:
    function alnum_and_spaces$string )
    {
        
    $string str_replace' '''$string );
        return 
    ctype_alnum$string );


  • Users who have thanked kbluhm for this post:

    Jacka (02-20-2008)

  • #3
    New Coder
    Join Date
    Dec 2006
    Location
    GB
    Posts
    79
    Thanks
    3
    Thanked 1 Time in 1 Post
    Perfect, thank you very much.

  • #4
    New Coder
    Join Date
    Dec 2006
    Location
    GB
    Posts
    79
    Thanks
    3
    Thanked 1 Time in 1 Post
    I need a bit more help.

    PHP Code:
    Removed 
    With the both ctype that you suggested and the regex, it works fine unless there is a space.

    Is there any way to allow spaces in the input?
    Last edited by Jacka; 02-20-2008 at 03:17 PM. Reason: Removed some unwanted code

  • #5
    New Coder
    Join Date
    Dec 2006
    Location
    GB
    Posts
    79
    Thanks
    3
    Thanked 1 Time in 1 Post
    Wait a minute, I just realised that I can removed spaces and check for the presence of symbols, then just use the original input!

    Thank you again kbluhm.

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Yeah, just use the function I gave you. It won't change the original input value:
    PHP Code:
    $_POST['input'] = 'This is alphanumeric and spaces only'// sent from form input

    if ( alnum_and_spaces$_POST['input'] ) )
    {
        echo 
    'good';
    }
    else
    {
        echo 
    'bad';


  • #7
    New Coder
    Join Date
    Dec 2006
    Location
    GB
    Posts
    79
    Thanks
    3
    Thanked 1 Time in 1 Post
    PHP Code:
    <?php
    $input 
    ucwords(strtolower(str_replace(array("_""-"), " "$_POST['input'])));

    if(
    ctype_alnum(str_replace(" """$input)) && strlen($input) <= 12){
        echo 
    "{$input} added to $table in $db";
    }
    else{
        echo 
    "Invalid input";
    }
    ?>
    Is that OK?

  • #8
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Looks like it will work.


  •  

    Posting Permissions

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