Results 1 to 4 of 4

Thread: php regex help

  1. #1

    php regex help

    hello forum, I am new here. I am also new to php and regex. I have been fighting with pho regex to filter input for names and nothing is working. I am tired of fighting. I cannot have a name field that doesn't have some sort of restriction. I have no idea how to limit the post to the following:

    any name in international letters and latin letters. no numbers, no spaces etc.
    I can eliminate spaces with !ctype_space($_POST['firstname']) and I can eliminate control characters and punctuation with ctype_punct and ctype_cntrl. However, these only work if ALL input is of that type. I can still enter space space space 001.

    I've tried "/^[\s.-'\pL]{1,40}$/" but my php page fails to load. I am sick of fighting this.

    surely someone knows how to allow a-zA-Z spaces apostrophe hypen period and international characters.
    I just want to eliminate numbers and certain characters.

    Jean-Luc is ok, O'Niell is fine, Smith Jr. is good
    M@tthew is not good. Y0uar3@L0ser is bad.

    anyone can help me? please.

  2. #2

    php regex help

    A bad regex will not prevent a page from working. That problem is somewhere else. Probably a syntax error.

    \pL requires using UTF-8 mode, which is enabled with the /u flag. Obviously your inputs have to be in UTF-8 for this.

    And your description says no spaces but your regex allows spaces, so that's wrong.

  3. #3

    php regex help

    yes, I forgot that I noticed a missing parenthesis last night. But the regex still allows numbers and special characters. My understanding of alphanumeric is alphabetic letters [a-zA-Z] or umlaut etc. and numeric is numbers. Apparently an at sign is alphanumeric or a letter. I am trying to eliminate ALL spaces alone (!ctype_space) or spaces with numbers and punctuation etc.
    basically, how can we limit to alphabetic letters but not numbers, punctuation or characters as hash/pound or at sign? is this not possible without a million if statements? I've actually used a bunch of if statements and three different programmers say that it can be 'simplified' but they never mention how to simplify it. I suppose a regex but nothing is working. I still get numbers and atsign accepted.

    this is the last regex that I tried and the M@tthew gets by it.
    && !preg_match("/^[.-'\p{L}]{1,40}/u$/", $_POST['firstname'])

  4. #4

    php regex help

    1. The regex is invalid because of how you tried to add the /u flag. Can you see the problem?
    2. Hyphens in character sets tend to mean a character range. .-' will mean period, apostrophe, and every character in between. If you want a literal hyphen then escape it.

    Both of those would have created warnings. If you didn't see the warnings then you do not have PHP set up properly for development. Find your php.ini, set error_reporting=-1 and display_errors=on, then restart your web server and try your current code again.

Posting Permissions

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