Hi Bill, can you please help on this regex? - Page 2

Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: Hi Bill, can you please help on this regex?

  1. #11
    Join Date
    Dec 1969
    Posts
    96,033

    Default

    I think the first thing I would do is check the length of the value. If it is 5 characters or less, do not even *ATTEMPT* to do any replacements.

    That will make doing the replacements easier.

    So:
    Code:
    $id = $_GET["id"];
    if ( strlen( $id ) >= 6 )
    {
        ... only now use regexp's ...
    }
    Start with that. Play with it. I'll play with it some, too.

  2. #12
    Join Date
    Dec 1969
    Posts
    708

    Default

    ok. thank you very much sir.

  3. #13
    Join Date
    Dec 1969
    Posts
    708

    Default

    Hello Bill,

    Using the if (strlen...) approach you came up with, along with the original regex I posted on page 1, now, I am able to get ID formats as well as zip code working correctly.

    Can you come up with another clever way to handle address?

    As you know, address could come in the form of 780 Bill Way or something like that and given that we have a rule that a space needs to be inserted after the first 2 digits, then 780 Bill Way could look more like 78 0 Bi LL way.

    Is there a way to indicate that if the digit is not up to 6 in length, no formatting required?

    This way, not only does it handle zip code, it will handle addresses as well?

    Thank you sir for all your help

  4. #14
    Join Date
    Dec 1969
    Posts
    96,033

    Default

    Oh, sure. Instead of just looking for strlen($id) < 6, instead use a regular expression to allow you to count ONLY digits.

    Don't trust me on this; experiment. But maybe this:
    Code:
    if ( strlen( preg_replace( '[^\d]', "", $id ) ) > 5 )
    {
       ... do the other replaces ...
    }
    See the logic? That if statement says: "Take the $id and replace EVERY non-digit character with the blank string; then get the length of what remains--which will be all digits--and only continue if it is longer than 5 characters."

    *** BUT ***

    What about an address such as
    1313 East 12th Ave
    ??
    Strip that to all digits and you have 131312 which is 6 digits. *KABLOOEY*

    Maybe a better way would be to count the *NON* digit characters and reject if there are 4 or more. (3 is okay...could be 33N888111GG881, meaning non-digit is NGG) *BETTER*, just count the characters that are *NOT* N or D or G or space!!

    So it still needs more work.

    *POSSIBLY* this:
    Code:
    if (    strlen( preg_replace( '[^\d]', "", $id ) ) > 5 
         && strlen( preg_replace( '[NnDdGg\d\s]', "", $id ) == 0 )
    {
        ... process more ...
    }
    That says "If there are 6 or more digits *AND* there are *NO* characters other than N, D, G, space, and digits *THEN* do more processing."

    No guarantees. Experiment.
    Last edited by Bill Wilkinson; 02-01-2013 at 01:24 AM.


  5. #15
    Join Date
    Dec 1969
    Posts
    708

    Default

    No sir, none of them worked.

    This one below is very, very close to working though.

    It displays addresses perfectly. It also displays zip codes exactly like we would want them displayed.

    The problem is that now, the id doesn't work anymore.

    The reason for that is that it gives the 2 spaces before GG as expected but it isn't giving any spaces after 2 digits if the digits have no N or D.

    That's what I am struggling with now.

    Code:
    $patterns = array(
        '/^(\d\d)(\d{4,})$/',
        '/^(?=.{6,}$)(\d\d[\dDN]\d*)(GG\d*)$/'
    );
    
    $replacements = array( '$1 $2', '$1  $2' );
    
    $id= preg_replace( $patterns, $replacements, $_GET[id] );
    Last edited by IheChi; 02-01-2013 at 12:54 PM.

  6. #16
    Join Date
    Dec 1969
    Posts
    96,033

    Default

    The second pattern in the array makes no sense to me.

    I would do this in steps, instead of trying to do it all at once.
    Code:
    $id = $_GET["id"];
    if (    strlen( preg_replace( '[^\d]', "", $id ) ) > 5 
         && strlen( preg_replace( '[NnDdGg\d\s]', "", $id ) == 0 
    ) {
        $id = preg_replace('(\d\d)([\dGg]+)', '$1 $2' );
        $id = preg_replace('(\d\d[NnDd\s]\d+)([Gg][Gg]\d+)', '$1  $2');
    }
    STOP GOING AT GETTING THE ID from $_GET at every step!

    Get it *ONCE* at the beginning, only. As shown there.

  7. #17
    Join Date
    Dec 1969
    Posts
    708

    Default

    It all working fine now sir.

    Thanks so much for all your help. It doesn't matter what I came to you with, you always find a way to help.

    I really appreciate it very much.

Posting Permissions

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