Say I have the following string
$str = "once in a great while a good-idea turns great";
What would be the best solution to creating an array with the array key being the string count of where the word(s) starts?
$str_array['0'] = "once";
$str_array['5'] = "in";
$str_array['8'] = "a";
$str_array['10'] = "great";
$str_array['16'] = "while";
$str_array['22'] = "a";
$str_array['24'] = "good-idea";
$str_array['34'] = "turns";
$str_array['40'] = "great";
The array_unshift() function inserts new elements to an array. The new array values will be inserted in the beginning of the array.
You can use the substr function like this: echo substr($myStr, 0, 5);
There is no specific function to prepend a string in PHP. But you can use the PHP concatenation operator ( . ) to prepened a string with another string.
As simple as the following:
str_word_count($str, 2);
what str_word_count()
does is
str_word_count()
— Return information about words used in a string
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With