Results 1 to 6 of 6
  1. #1

    some preg_match questions

    how with preg_match do you allow something to match certain special characters but not normal chars and vise versa. Also how do you matchall special chars and not normal chars?

  2. #2
    You might want to change your title as this is more of a regex question than a preg_match question, right? PHP.Net only shows you how the function works.

    Code:
    <?php
    $subject = "abcdef";
    $pattern = '/^def/';
    preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
    print_r($matches);
    ?>
    Any guru can correct me if I am mistaken, but I think brackets [] are the most effective way to state exactly what you want to allow, meaning the preg_match will only match those special characters listed and not normal chars (I'm assuming you mean A-Za-z0-9 when you say normal chars)

    Here is an example:
    Code:
    preg_match("/^[^a-zA-Z0-9]+$/", $_POST['stringvar']); //match anything that isn't a-z A-Z or 0-9
    \W I think is the equiv of this, but I've never used it as I've never had to check for special characters only ^_^;;


    my favorite regex tutorial:
    http://gnosis.cx/publish/programming...pressions.html


    Hope this information helps.
    Best Hosting Experiences: Voxtreme.Com (Shared Reseller / 5yrs / Former), Serverpoint.Com (Dedicated & VPS / 13+ yrs / Current)

  3. #3
    well put it this way i want to check an e-mail address but i dont want to allow special characters yet i seem to be able while testing to slip some through

  4. #4
    Join Date
    May 2004
    Location
    Singapore
    Posts
    262
    One way to approach this would be to test for valid sequences, i.e. you state what characers are valid, rather than state what characters are invalid.
    #include<cstdio>
    char*s="#include<cstdio>%cchar*s=%c%s%c;%cint main(){std::printf(s,10,34,s,34,10);}";
    int main(){std::printf(s,10,34,s,34,10);}

  5. #5
    Code:
    if(!preg_match("/^[a-z0-9-_.]+@[a-z0-9-]+.[a-z.]+$/", strtolower($_POST['email']))){
        //shoot the registration form back to error page or something.
        exit;
    }
    laserlight is 100% correct. I just wasn't sure what you wanted to validate so I suggested the NOT option ^_^;;

    Above is my code for verifying emails on my registration page. I dunno how you return form errors, so I'll leave the // to your own code ^_^

    Hope this information helps.
    Best Hosting Experiences: Voxtreme.Com (Shared Reseller / 5yrs / Former), Serverpoint.Com (Dedicated & VPS / 13+ yrs / Current)

  6. #6
    Oh, btw, you can test it on your own or test my registration page (at the risk of sounding rude, please do NOT register, just test the email ^_^;;) If you like the way the code works, use it. If special chars slip through... please let me know! *cringe*

    http://www.ochiba.net/register.php

    You should turn off javascript to test the PHP preg_match ^_^;; I forgot I had javascript on there (I did that to save on bandwidth)

    You don't need to fill out the entire form, just a name and the bogus email.
    Last edited by ochiba; 10-08-2005 at 12:50 PM.
    Best Hosting Experiences: Voxtreme.Com (Shared Reseller / 5yrs / Former), Serverpoint.Com (Dedicated & VPS / 13+ yrs / Current)

Posting Permissions

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