I have a list of names and each name consist of 2-4 words: name, (if exist) middle name(s), surname.
These are the names:
- Ali Yilmaz
- Taha Ugur Unal
- Omer Ibrahim Tahsin Son
- Recai Sahin
etc.
I want to print this names as this: Surname, name middle name(s) (if exist)
The names above will be:
- Ali Yilmaz -> Yilmaz, Ali
- Taha Ugur Unal -> Unal, Taha Ugur
- Omer Ibrahim Tahsin Son -> Son, Omer Ibrahim Tahsin
- Recai Sahin -> Sahin, Recai
etc.
If I had only one name I can use that code:
<?php $exp1 = explode(" ", $author1); ?>
<?php if (count($exp1) == 2) {?>
<?php print ($exp1[1] .', ' .$exp1[0]); ?>
<?php } elseif (count($exp1) == 3) {?>
<?php print ($exp1[2] .', ' .$exp1[0] .' ' .$exp1[1]); ?>
<?php } elseif (count($exp1) == 4) {?>
<?php print ($exp1[3] .', ' .$exp1[0] .' ' .$exp1[1] .' ' .$exp1[2]); ?>
<?php }?>
Each page can have different numbers of author and I thought I could use foreach to apply the above code for each author name but I couldn't do this.
I tried a piece of code such that:
<?php foreach $author as $obj): ?>
<?php $a = explode (" ", $obj) ?>
<?php endforeach ?>
...
But it gives error:
explode() expects parameter 2 to be string, array given.
How can I do this?