SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: preg question

  1. #1
    SitePoint Zealot
    Join Date
    Nov 2005
    Location
    Southern Nevada, USA
    Posts
    150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg question

    I have a second address line in a form and I want it to be able to be empty, or contain only letters, numbers or one of the following ". , : /"

    Here is the code I came up with, but it doesn't work.

    Code:
    if(preg_match("/^\d{0}$/|[a-zA-Z0-9 _.,:\"\']+$/", $_POST["addr2"]) === 0)
    I don't have a good understanding of the preg syntax, and I put this code together from different examples.

    The warning I get is that the "or" symbol "|" is an unknown modifier.

    Where am I going wrong?

    Lawrence

  2. #2
    SitePoint Zealot
    Join Date
    Nov 2005
    Location
    Southern Nevada, USA
    Posts
    150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I kept playing with this code and come up with this:

    Code:
    if(preg_match("/^\d{0}|[a-zA-Z0-9 _.,:\"\']+$/", $_POST["addr2"]) === 0)
    This appears to work. Is there anything wrong with this code for what I want to do?

    Lawrence

    Edit: Further testing shows this does now stop characters such as *()^%$#.
    So I guess I still need help.

    Since I am stuck on preg, I have another one where I need 0 to 50 characters consisting of letters, numbers or one of the following ". , : /"

    I would like to learn how to structure these correctly and would appreciate any suggestions on tutorials. I have four books on PHP, and only two of them briefly mention "preg".

    Lawrence

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    Sydney, Australia
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try

    PHP Code:
    $addr2 trim($_POST["addr2"]);
    if(empty(
    $addr2) || !preg_match("/^([\w \.,:\"'\*\(\)\^%\$#]+)$/i"$addr2)) {

    In the preg_match, there are special characters that have specific meaning to the preg_match function, such as *()[]^$+ etc. I've escaped these characters with a backslash.

    Regular Expressions are quite difficult at first, The php manual has some details (http://au.php.net/manual/en/regexp.reference.php), however there are books dedicated to the subject, try amazon.

  4. #4
    SitePoint Zealot
    Join Date
    Nov 2005
    Location
    Southern Nevada, USA
    Posts
    150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Bling View Post
    Try

    PHP Code:
    $addr2 trim($_POST["addr2"]);
    if(empty(
    $addr2) || !preg_match("/^([\w \.,:\"'\*\(\)\^%\$#]+)$/i"$addr2)) {

    In the preg_match, there are special characters that have specific meaning to the preg_match function, such as *()[]^$+ etc. I've escaped these characters with a backslash.

    Regular Expressions are quite difficult at first, The php manual has some details (http://au.php.net/manual/en/regexp.reference.php), however there are books dedicated to the subject, try amazon.
    Thank you.

    I was just looking at some of the books on this subject on Amazon.

    Lawrence

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Not sure if any use, but this'll strip anything not on the list, so you can cleanse and continue.
    PHP Code:
    // rm all but Numbers, letters and '., /'
    $input '0123 Big Street bc < &lt; ?.,/#';
    $output preg_replace('#[^0-9a-z \.\,\/]#i'''$input);
    echo(
    $output); 
    (I am very interested if anyone finds this flawed, by the way)
    Last edited by Cups; Jan 7, 2009 at 03:55. Reason: Drat, having a problem with backslashes now ...

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Not sure if any use, but this'll strip anything not on the list, so you can cleanse and continue.
    PHP Code:
    // rm all but Numbers, letters and '., /'
    $input '0123 Big Street bc < &lt; ?.,/#';
    $output preg_replace('#[^0-9a-z \.\,\/]#i'''$input);
    echo(
    $output); 
    (I am very interested if anyone finds this flawed, by the way)
    I'm pretty sure you don't need to escape the comma.

    PHP Code:
    <?php
    #find anything that is NOT a letter, number, space or (.,:/).
    $sPattern '~[^\\sA-Z0-9\\.,:/]~im';
    $sSubject 'Some Address';
    if(
    === preg_match($sPattern$sSubject))
    {
        
    #valid
    }
    else
    {
        
    #invalid
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Evangelist
    Join Date
    May 2006
    Location
    Denmark
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't need to escape the dot either. It doesn't function as a metacharacter within a character class.

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    // rm all but Numbers, letters and '., /'
    $input '0123 Big Street bc < &lt; ?.,/#';
    $output preg_replace('#[^0-9a-z .,/]#i'''$input);
    echo(
    $output); 
    Yeah, thats strange, I had to add the slashes in the regex to post it, but now it works?

    Anyhow ... I posted that because it allows the developer to strip out what is not allowed, rather than taking the boolean "disallowed" decision.

    It all depends how kind you want to be to your users.

    I can also see value in this method I read about on a blog today talking about making clean url slugs:

    "One can quickly see that a lot of characters will disappear from the URL. Imagine some accented characters (&#232;, &#235; etc.) in the page title. These will be removed by the first line of the function since they are not exactly url-friendly. What we actually want is to convert these characters to their base-character, meaning that &#232; would become a regular e etc.

    PHP Code:
        if (function_exists('iconv')) {  
             
    $string = @iconv('UTF-8''ASCII//TRANSLIT'$string);  

    That'd be even kinder, Y'know if someone live at "24 Rue de Caf&#232;".

    I must give it a go.

  9. #9
    SitePoint Zealot
    Join Date
    Nov 2005
    Location
    Southern Nevada, USA
    Posts
    150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can I ask, what is wrong with this statement?

    Code:
    if((!empty($addr2)) && (preg_match("/^[a-zA-Z0-9 _.,:\"\']+$/", $_POST["addr2"]) === 0))
    What I am trying to do is to print an error that says the user must go back and correct the input for addr2 if addr2 is not empty and does not match the preg_match statement. If addr2 is empty, I don't want to print the message.

    Lawrence

    Edit: I found the problem. It should have read:

    [CODE]if (!empty($_POST["addr2"])&& (preg_match("/^[a-zA-Z0-9 _.,:\"\']+$/", $_POST["addr2"]) === 0))[CODE]


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
  •