Spliting and storing string in an array php -
i have string
$descr = "hello test string";
what trying split string , store each word separated using space separate array index in php. should use
$myarray = preg_split("[\s]",$descr);
expected outcome :
$myarray(1) : hello $myarray(2) : $myarray(3) : $myarray(4) : $myarray(5) : test $myarray(6) : string
each number denotes array index
you need use explode() below:-
$myarray = explode(' ', $descr); print_r($myarray);
output:-https://eval.in/847916
to re-index , lowercase each word in array this:-
<?php $descr = "hello test string"; $myarray = explode(' ', $descr); $myarray = array_map('strtolower',array_combine(range(1, count($myarray)), array_values($myarray))); print_r($myarray);
output:-https://eval.in/847960
to how many elements there in array:-
echo count($myarray);
Comments
Post a Comment