Count string characters

Asked

Viewed 1,381 times

5

I am counting the characters of a string in PHP. The content of the string is: 10,12,12,22,33. I want to percolate to print one by one and with an " n". The problem is that I use strlen, and it counts all characters including commas. I wanted to count a character only after the comma. The code I used:

$max= strlen($positionY); // 6
for($i=0; $i<$max; $i++){
    fwrite($hndl, $positionY[$i]);
    fwrite($hndl, "\n");
}

1 answer

11


From what I could understand of the question, you want to divide the string by comma and count the quantity, this way:

$strTxt = '10,12,12,22,33';
$arrDividido = explode(',', $strTxt);
$intQtde = count($arrDividido); // Tem a quantidade de textos separados por virgulas

foreach($arrDividido as $strDiv) {
  echo $strDiv . '<br />';
}
/* Saida
10
12
12
22
33
*/

Functions used:

Count()

explode()

  • 1

    That’s right, it works perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.