SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Mar 2010
    Location
    UK
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Find the number of matches from a reg.ex.

    I have a question about how regular expressions and PHP may be able to count the number of successful matches and output it or only do something once a minimum number of matches are found.

    I am aware of a way of limiting a regular expression using something like {2,5} but I cannot get that to work, so I suppose that my only option is to use some sort of if statement that counts the number of matches from the regular expression and then does something if that number meets or exceeds what I specify.

    But how do I get that magic number?

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,897
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Take a look at preg_match_all. More specifically take a look at what it returns:

    Returns the number of full pattern matches (which might be zero), or FALSE if an error occurred.
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Addict
    Join Date
    Mar 2010
    Location
    UK
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've heard of that function and I've tried it before, but I couldn't work out how to use it correctly. It just kept breaking my code.

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,897
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Could you post the code you tried so we can have a look?
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Addict
    Join Date
    Mar 2010
    Location
    UK
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, I didn't keep it. I struggled with the extra parts that preg match has, and how to get the result out of it.

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,897
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Okay. Allow me to give an example then:

    PHP Code:
    $str 'When we test this test it is testing whether the test is correct.';
    if (
    preg_match_all('~test~'$str$matches) > 3) {
       echo 
    '"Test" occurs more that 3 times';
    } else {
       echo 
    '"Test" occurs 3 times or less';

    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    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)
    Does this clear things up any?
    PHP Code:
    <?php
    preg_match_all
    ('~[a-z]+~''abc12d3ef456g7'$matches);

    var_dump(
      
    $matches
    );

    /*
      array(1) {
        [0]=>
        array(4) {
          [0]=>
          string(3) "abc"
          [1]=>
          string(1) "d"
          [2]=>
          string(2) "ef"
          [3]=>
          string(1) "g"
        }
      }
    */
    @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.

  8. #8
    SitePoint Addict
    Join Date
    Mar 2010
    Location
    UK
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ScallioXTX, thanks for that example. That has helped and I think that I'll be able to use that for what I need.

    AnthonySterling: your example is a bit beyond my level, but thanks for responding.


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
  •