SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Perl Regex not working

    I am looking to capture the users firstname, and I only want to capture characters so im using the preg_match function. The problem is with the code below, im not getting a match when typing in a simple name such as "john".

    //Clean user input
    $inString = $_POST['alias'];
    $cleanAlias = clean_input($inString) ."<br />";

    //Peform Form Validation!
    $pattern = '/^[A-Za-z]$/';
    $subject = $cleanAlias;

    if(preg_match($pattern, $subject))
    {
    echo "match found";
    }
    else
    {
    echo "no match found";
    }

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    What you're asking for by using:
    Code:
    /^[A-Za-z]$/
    is the beginning, a single letter and then the end.

    If you want multiple letters:
    Code:
    /^[A-Za-z]*$/
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi, thanks for your reply! ive tried your version, but still not match

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    55 Post(s)
    Tagged
    0 Thread(s)
    Given the code in your first post, the $subject variable will contain the string john<br /> (assuming the cleaning function returns john) which clearly contains characters not in the alphabet.
    Salathe
    Software Developer and PHP Manual Author.


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
  •