Count characters string

Posted on

Coding Problem :

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

Answer :


Answer 1 :

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

    


Answer 2 :


Answer 3 :


Answer 4 :


Answer 5 :


Answer 6 :


Answer 7 :


Answer 8 :


Answer 9 :

Leave a Reply

Your email address will not be published.