1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

counting strings in foreach

Discussion in 'PHP' started by bumbar, Jan 11, 2016.

  1. #1
    Hello,
    I have a string that divide it into pieces with a comma.
    Each piece divide it into smaller interval with.
    You have to count how many pieces with interval there are in every piece with comma below.
    
    $string = "1str 1str, 2str 2str, 3str 3str";
    $keywords = explode(',', $string);
    foreach ($keywords as $v) {
        $parts = explode(' ', $v);
        echo count($parts) . "<br>";
    }
    
    Code (markup):
    Outputs the 2 3 3, not what it expect 2 2 2
    Where is the problem?
    Why counting does not work properly?
    Thanks!
     
    bumbar, Jan 11, 2016 IP
  2. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #2
    The problem is the 2nd time your telling it to split where there is a space. And you have a space before and after the words for the 2nd and 3rd set. try this and see.


    
    $string = "1str 1str,2str 2str,3str 3str";
    $keywords = explode(',', $string);
    foreach ($keywords as $v) {
        $parts = explode(' ', $v);
        echo count($parts) . "<br>";
       
    }
    PHP:
     
    KangBroke, Jan 11, 2016 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    This will work:
    
    <?php
    $string = "1str 1str, 2str 2str, 3str 3str";
    $keywords = explode(',', $string);
    foreach ($keywords as $v) {
      $parts = explode(' ', ltrim($v));
      echo count($parts) . "<br>";
    }
    
    ?>
    
    Code (markup):
    without changing the source
     
    Last edited: Jan 12, 2016
    PoPSiCLe, Jan 11, 2016 IP
    KangBroke likes this.
  4. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #4

    PHP - There is a function for everything. I pointed out why you were getting the wrong count but PopSiCLe gave you the solution that you want to use.
     
    KangBroke, Jan 11, 2016 IP
  5. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #5
    So perhaps even take it one step further ?


    $string = "1str 1str , 2str 2str , 3str 3str ";
    $keywords = explode(',', $string);
    foreach ($keywords as $v) {
      $parts = explode(' ', rtrim(ltrim($v)));
    
    echo count($parts) . "<br>";
    }
    
    PHP:
     
    KangBroke, Jan 11, 2016 IP
  6. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #6
    Without having to worry about extra spaces/variables:

    
    foreach (explode(',', "1str 1str , 2str 2str , 3str 3str ") as $v) {
        echo str_word_count($v);
    }
    
    PHP:
     
    ThePHPMaster, Jan 12, 2016 IP
    deathshadow and KangBroke like this.
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    Would you look at that :D Didn't even know that function existed.
     
    PoPSiCLe, Jan 12, 2016 IP
  8. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #8
    The joke I've made for quite some time:

    PHP, yeah... there's a function for that.

    @KangBroke, I'm kind of laughing at the use of rtrim around ltrim... when there's just plain trim that does both ends at once!
    http://php.net/manual/en/function.trim.php
     
    deathshadow, Jan 12, 2016 IP
    KangBroke likes this.
  9. sajjadhira

    sajjadhira Greenhorn

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #9
    Use this for count all,
    $string = "1str 1str, 2str 2str, 3str 3str";
    $keywords = explode(',', $string);
    foreach ($keywords as $v) {
    $parts = explode(' ', $v);
    $count+=count($parts);
    }
    echo $count;
    PHP:
     
    sajjadhira, May 27, 2016 IP