SitePoint Sponsor

User Tag List

Results 1 to 15 of 15

Thread: simple regex!

  1. #1
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need some help with regex! I would like to get a little bit of code that I could use to do this.. and what I have is NOT working! What I'm trying to do is check if something ONLY had alpha numeric charachters and is allowed dashes (-) and decimal points (.)

    So, I just threw this into a simple if/else, and it's not working!


    PHP Code:
    if(ereg("\.a-zA-Z0-9-",$cartridge)) {
    $value 1;
    } else {
    $value 0;

    thanx for any help!
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  2. #2
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try placing brackets around some of those...like this:

    a-z becomes [a-z]

    I could be wrong, but I think that's how RegExp's are usually formatted in PHP.

  3. #3
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The pattern should be:
    Code:
    "[\.a-zA-Z0-9-]"
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  4. #4
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you! I wasn't sure.. anyone have a good, easy to understand for basic PHP losers, tutorial for regex's?

    thanx again, god bless
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  5. #5
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That pattern will match if the string contains any of the required characters, not if it contains only the required characters.

    "^[\.a-zA-Z0-9-]+$" will check that the string contains one or more of the required characters only.

    "^[\.a-zA-Z0-9-]*$" will check that the string contains zero or more of the required characters only.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Babys got backa hard back, that is: The Ultimate CSS Reference

  6. #6
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The regexps described above work perfectly, but for future reference it's useful to know why they worked (I struggled with a very similar exp recently and the solution is great once you've got it sussed).

    As you know, [a-z] will match anything in the range from a to z. The problem with your expression is that you need to match the - character on its own. Obviously if you stick this in between square brackets the PHP interpretor will get confused and think you mean the symbol as part of a range description. The solution here is to put it right at the end of the stuff in the square brackets with nothing to the right of it. This ensures that it could not possibly be part of a range.

    As for the full stop, a . is regexp talk for "match any single character". To indicate that you want to match a real "." you need a back slash to 'escape' it i.e "\."

    Hope that clears things up,

    Skunk

  7. #7
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thnk you skunk! I already knew about the escaping of special characters, and about the (-). Thanx though! Anyone have a good tutorial?

    thanx again kevin... that's what I really needed!
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  8. #8
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    still not working!

    I tried all ideas of Kevins.. every method and combination I could.. and it's still not working. Right now I always get the error.. no matter what I enter!

    Here's my code.. what I'm trying to do is only allow Alpha numeric chars, periods (.) and dashes (-).

    PHP Code:
    if(ereg("^[\.a-zA-Z0-9-]+$",$cartridge)) {
    $value 1;
    } else {
    $value 0;
    }

    if(
    $value == 0){
    echo(
    "Illegal Characters");
    } else {

    // Add to database
    // Display confirm

    This does not accept anything! Any ideas? I also tried this:

    PHP Code:
    if(ereg("^[\.a-zA-Z0-9-]*$",$cartridge)) {
    $value 1;
    } else {
    $value 0;
    }

    if(
    $value == 0){
    echo(
    "Illegal Characters");
    } else {

    // Add to database
    // Display confirm

    This also rejects anything... please help!

    Here is an example of what I want to be entered:

    .30-30 Ackley Improved

    NOT:

    .30-30 Ackley Imp'

    Thank's to all...

    God Bless

    Alex
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  9. #9
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This worked for me, I basically created a function that returns te result of the ereg. It will either return 0 or 1 depending on whether it found what it was looking. So you can test a string to see if it contains only the specified characters.

    PHP Code:
    <?

        
    function check_string($str) {
            return 
    ereg("^[\.a-zA-Z0-9-]+$"$str);
            }

        
    $str "s-fghs.jf-ghs.f";
        if (
    check_string($str)) {
            print 
    "Okay";
            }
        else {
            print 
    "Error";
            }
            
    ?>
    Last edited by freddydoesphp; Mar 13, 2001 at 00:01.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  10. #10
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    doesn't the decimal have to be escaped?
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  11. #11
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah it just doesn't show up in the forum
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  12. #12
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    still no luck!

    I'm running PHP 4.x, and I'm still having problems with this!

    Using freddy's script as well as my own and kevin's I'm still not having any luck... with freddy's it's not letting me get ANYTHING through it! Please help... What I'm really trying to do, if it would be easier, is not to include these:

    !@#$%^&*()_+=|\}{]["':;?/><.,~`

    thanx a bunch for all of the help... I guess I just thought it would be easier to just ONLY accept [\.A-Za-z9-0-]

    Please help..

    Thanxs much!

    Alex
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  13. #13
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Exbabylon,

    You forgot to mention that your strings can contain spaces as well! Just add a space to the list of acceptable characters and you should be good to go:

    "^[\. a-zA-Z0-9-]+$" or
    "^[\. a-zA-Z0-9-]*$"
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Babys got backa hard back, that is: The Ultimate CSS Reference

  14. #14
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you much! I learned a lesson this week on Regular Expressions! Thank you to all who helped me to advance my knowledge.
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  15. #15
    SitePoint Enthusiast
    Join Date
    Nov 2000
    Location
    Allentown PA
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One thing that's worth noting: within the brackets, you can define characters you DON'T want to allow with the ^ (yes, it has 2 meanings)

    [^ \.a-zA-Z0-9-] will match any character OTHER THAN a space, a period, a-z, A-Z, 0-9, or a dash.

    So this is how I'd do it personally:
    Code:
    if(ereg("[^ \.a-zA-Z0-9-]",$cartridge)) {
      //it contains a bad character
      $value = 0;
    } else {
      $value = 1;
    }
    
    if($value == 0){
      echo("Illegal Characters");
    } else {
      // Add to database
      // Display confirm
    }


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
  •