SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: counting chars

  1. #1
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Rome
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    counting chars

    Hello

    suppose I have

    $test="hasop dame woddsau ha uiua";

    which is a fast way to check if there are more than 4 "a" in $test .
    For example the line above should return "true" because contains 5 "a".


    Thank you
    Graziano
    trcris ::> http://www.trcris.com

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $test 
    "hasop dame woddsau ha uiua";

    // returns 5
    echo substr_count($test"a");
    ?>
    so...

    PHP Code:
    <?php
    $test 
    "hasop dame woddsau ha uiua";

    function 
    chr_count($str$chr$limit) {
        return (
    substr_count($str$chr) > $limit) ? true false;
    }

    $c chr_count($test"a"4);
    // $c = true;
    ?>

  3. #3
    SitePoint Enthusiast PicklePete's Avatar
    Join Date
    Apr 2008
    Location
    England
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's probably a better way, but this works.

    PHP Code:
    <?php
        $haystack 
    'aaabbc';
        
    $needle 'a';
        
        for(
    $i 0$i strlen($haystack); $i++)
        {
            if(
    $haystack[$i] == $needle)
            {
                
    $count++;
            }
        }
        
        ((
    $count 0) ? $in_stack TRUE $in_stack FALSE); 
    ?>

  4. #4
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Rome
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you !

    Does exist also a solution using ereg ?
    trcris ::> http://www.trcris.com

  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)
    I'm not entirely sure if there is a native function, but here's a quick custom one.
    PHP Code:
    <?php
    function countChars($sString$sChar)
    {
        
    $iCharacterCount 0;
        for(
    $iMarker 0$iMarker <= strlen($sString); $iMarker++)
        {
            if(
    $sString[$iMarker] === $sChar)
            {
                
    $iCharacterCount++;
            }
        }
        return 
    $iCharacterCount;
    }

    $sString 'hasop dame woddsau ha uiua';
    echo 
    countChars($sString'a'); #5
    ?>
    @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
    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)
    Quote Originally Posted by graziano68 View Post
    Does exist also a solution using ereg ?
    Sure.

    PHP Code:
    <?php
    function countChars($sString$sChar)
    {
        return 
    preg_match_all(sprintf('~%s~'preg_quote($sChar)), $sString$aMatches);
    }

    $sString 'hasop dame woddsau ha uiua';
    echo 
    countChars($sString'a'); #5
    ?>
    @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.

  7. #7
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Rome
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you
    trcris ::> http://www.trcris.com

  8. #8
    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)
    In case any one was wondering the preg solution is way faster.

    Over 100,000 iterations.

    • 1.86774 #non-preg
    • 0.79546 #preg
    @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.


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
  •