Count characters string

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 scroll to print one by one and with a "\ n". The problem is that I use strlen, and it counts all the characters including the commas. I wanted to count a character just after the comma. The code I used:

$max= strlen($positionY); // 6
for($i=0; $i<$max; $i++){
    fwrite($hndl, $positionY[$i]);
    fwrite($hndl, "\n");
}
    
asked by anonymous 17.12.2014 / 11:46

1 answer

11

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

$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 ()

    
17.12.2014 / 12:27