Cut word after comma

Asked

Viewed 68 times

-1

I have a code to cut the words after the comma. But how do I know how many string will be generated by the cutter ? For one can put 5 words, as one can put 10 at once.

$str  = "valor1,valor2,valor3";
$corta = explode(",", $str);
echo $corta[0];
echo $corta[1];
  • For that you should use one loop which will print the result N times according to the size of your array

  • You can use the count to know how many elements there are in the array, that is, how many words: count($corta)

1 answer

5


Use Count, to count the array elements, and then you can loop to your liking

<?php
$str   =  "valor1,valor2,valor3";
$corta  =  explode(",", $str);
for($i=0 ; $i  < count($corta); $i++)
{
    echo  $corta[$i]."\n";
}
?>

Example

  • How can I save each cut separately in the database ? I tried the following, but it didn’t work ( saved only the last string ): $str = "$a_word"; $cuts = explodes(",", $str); for($i=0 ; $i < Count($cuts); $i++) { $res_word = $cuts[$i]; echo "$res_word"; $insert_word = $mysqli->query("INSERT INTO filtro_word (word) VALUES ('$res_word')");

  • @Codegtechsystems you must give insert within your loop, agrees that it is within your loop in which you have access to cortes separately? I think it is worth giving a study in loops as for, while, etc..

  • You can open a new question, check [help] to see how best to do it. Friend tip is valid.

Browser other questions tagged

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