SitePoint Sponsor

User Tag List

Results 1 to 15 of 15

Thread: REGEX question?

  1. #1
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Arrow REGEX question?

    Hello

    How do you remove any characters from a string except for numbers
    from 0-9?

    example;

    $string = '3s34d62.45,098,e43a,148975';

    and once filtered should look like this below,

    $string = '334624509843148975';

    Thank you.

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2009
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $string '3s34d62.45,098,e43a,148975';

    $string preg_replace('/[^0-9]/','',$string);

    echo 
    $string// 334624509843148975 

  3. #3
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    thanks alan22

  4. #4
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    How about i want to remove the digits after the period?

    example,
    $string = '193,000,260.54';

    so the result should look like this below,
    $string = '193,000,260';

    without the period and the 54 number.

    Thanks again in advance.

  5. #5
    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)
    Noticing the amount of RegExp questions you have, have you thought of learning the syntax?

    You'll find many of your current problems are quite basic so the time investment would be quickly repaid.

    Try to find a solution for your current issue and post your results here, maybe we could teach you the answer rather then provide it for you.

    @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.

  6. #6
    SitePoint Enthusiast nrg_alpha's Avatar
    Join Date
    Dec 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, while I would go with Alan's initial solution, one possible non-regex solution:

    PHP Code:
    $string '3s34d62.45,098,e43a,148975';
    for(
    $a 0$a strlen($string); $a++){
        if(!
    ctype_digit($string[$a])) $string str_replace($string[$a], ''$string);
    }
    echo 
    $string
    Granted, in this case, strlen() is recalculated due to string indexes getting the axe on passes where the current index isn't a digit. So not the best practice admittedly, but for single pass, the speed difference wouldn't be much.

    As for the next issue (regarding removing .54 from 193,000,260.54), I won't give the answer to honour SilverBullet's request. But if using PHP 5.3, I would consider making use of strstr().

    If not using PHP 5.3, then perhaps consider making use of strtok().

  7. #7
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    @SilverBulletUK

    Okay can you show me a nice, simple and easy to understand website tutorial.
    But with COMPLETE tutorial about REGEX.

    thanks in advance.

  8. #8
    SitePoint Enthusiast nrg_alpha's Avatar
    Join Date
    Dec 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  9. #9
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    thanks nrg_alpha.

    I'm thinking of buying Mastering REGEX By Jeffrey E. F. Friedl

    Is this book really a must?
    Is the internet free tutorial is not enough?

    Should i buy this book?
    and why?

    thanks in advance.

  10. #10
    SitePoint Enthusiast nrg_alpha's Avatar
    Join Date
    Dec 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, yes, if you want a book, then that book is THE bible! But if you want to start with free resources, those links will get you started.. but yes, if you really want to get on the ball with REGEX, then that book is HIGHLY recommended!

    EDIT - As for why, the book goes into great details about how the regex engine *thinks*.. how it processes things under the hood kind of thing.

  11. #11
    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)
    Sure, this site gives a good overview, I often find this live RegExp application quite useful also, not forgetting the PHP manual too.

    When putting together a pattern, I try to work out how I determine the value I want (or don't). Figure out a reliable sequence given the data provided that will always result in the desired outcome.

    Once you have this sequence, build a pattern to replicate it.
    @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.

  12. #12
    SitePoint Enthusiast nrg_alpha's Avatar
    Join Date
    Dec 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    When putting together a pattern, I try to work out how I determine the value I want (or don't). Figure out a reliable sequence given the data provided that will always result in the desired outcome.

    Once you have this sequence, build a pattern to replicate it.
    While I mirror this approach, I take into a c c o u n t (<-- post keeps censoring that word for some reason) the regex engine's pitfalls, as we don't want the pattern doing more work than necessary. As an example, many people use .* carelessly, not understading the potential pitfalls involving such a thing (speed, and even more importantly, accuracy come into possible play - it's dependant on the circumstances).

    By learning the inner workings of regex, one can formulate a pattern that accomplishes the same desired result as another pattern but more efficiently.

  13. #13
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    @SilverBulletUK
    I will consider your suggestion, i will review them in few days.
    Thanks.

    @nrg_alpha
    I noticed it's in 3rd edition, what's the difference between the 1st and the 3rd edition?

    Should i buy the 3rd edition? or the 1st edition?

    Thanks in advance.

  14. #14
    SitePoint Enthusiast nrg_alpha's Avatar
    Join Date
    Dec 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by solidcodes View Post
    @nrg_alpha
    I noticed it's in 3rd edition, what's the difference between the 1st and the 3rd edition?
    The third addition adds a full chapter on PHP PCRE (which my understanding was not included in the previous editions..) so yeah, I have that one. I think that is the latest one. You should always buy the latest edition.

  15. #15
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Arrow

    [QUOTE=nrg_alpha;4357010]The third addition adds a full chapter on PHP PCRE (which my understanding was not included in the previous editions..) so yeah, I have that one. I think that is the latest one. You should always buy the latest edition.[/QUOT

    Many thanks guys...

    From now on i'll try first experimenting with my own REGEX know how before asking you guys out there.

    Thanks.


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
  •