Having few problems with what should ideally be a simple task.
What I'm trying to do is replace the ', ' before the last word & .
So basically, if there is a word in $ddd than what you need, it is like DDD, and if $ddd empty than & CCC
Theoretically, what I need is the following:
"AAA, BBB, CCC and DDD" when all 4 words are not empty "AAA, BBB and CCC" when 3 are not empty and the last is "AAA and BBB" when 2 are not empty and the last 2 words are empty " AAA "when only one returns non-empty.
Here is my script
$aaa = "AAA"; $bbb = ", BBB"; $ccc = ", CCC"; $ddd = ", DDD"; $line_for = $aaa.$bbb.$ccc.$ddd; $wordarray = explode(', ', $line_for); if (count($wordarray) > 1 ) { $wordarray[count($wordarray)-1] = '& '.($wordarray[count($wordarray)-1]); $line_for = implode(', ', $wordarray); }
Please do not judge me, as this is just an attempt to create what I tried to describe above.
Please, help
AlexB source share