Hello friend, I have a small PHP exercise in which I have to request the user's name and tell him how many words his full name has and that's it.
but I do not know it, I have this following code:
$requestedtext = print "var name= prompt('what's your name');"; echo "Your name has:"; echo str_word_count($textrequested);//will return the number of words in a string
You can use
str_word_count
, available since PHP version 4, and in 5 and 7.The function receives as the first parameter your
string
, as the second an optional value to return only the word count0
or by default nothing, it1
returns an array with all the words found and their order, and2
which also returns an array but shows the words found and the position at which it is found within the string which you passed.Thus:
With a value
1
likeformato
:Passing
2
, this time shows the position within yourstring
where the second word begins:The third and also optional parameter is the
charlist
one that can be used to add an additional list of characters that will be used as words.