I asked recently a question and received very nice answers to solve my newbie doubts. Based on them and tips/tricks from several websites, I've been trying to get solved the following doubt but without success. Basically, I'd like to explode sentences (with variable number of words) and then implode it but with a certain pattern.
For instance:
input: coastal basin dr54 dr34 valley
output: this is @ word(coastal) or this is @ word(basin) or this is @ word(dr54) or this is @ word(dr34) or this is @ word(valley)
so, basically to add this is @ word()
for each word and then separate them with or
. In case the sentence has only one word (e.g., coastal), it should not appear or
(ie., this is @ word(coastal)
).
I explode the input as below but had no success to implode it as I want it:
php > $sentence = 'coastal basin dr54 dr34 valley';
php > print_r($sentence);
coastal basin dr54 dr34 valley
php > $words = explode(' ', $sentence);
php > print_r($words);
Array
(
[0] => coastal
[1] => basin
[2] => dr54
[3] => dr34
[4] => valley
)
Not sure if this is too simple, but if it is, please bear with me! I'm pretty new in php. Any hint is welcomed,