can't get ucfirst() to work on a variable
I have made a code that takes the first name and last name from an email, the $firstname is uppercase but $lastname is not. Why?
<html> <body> <?php $email = "[email protected]"; $firstname = ucfirst(strtok(strtok($email, "@"), ".")); $lastname = substr(strtok(strtok($email, "@"), ".") . ' ' . strtok("."), strrpos(strtok(strtok($email, "@"), ".") . ' ' . strtok("."), ' ')); $lastname = ucfirst($lastname); echo $firstname.$lastname; ?> </body> </html>
Output: Test testt
any help would be greatly appreciated
Besides the actual problem being the space, your code does a lot of searching and chopping strings up.
You could simplify it by using
explode() with first the
@ and then a
.. Then using
ucfirst on each part of the last operation...
$names = explode("@", $email); // Get first 2 parts of name and split it by the . [$firstname, $lastname] = explode(".", $names, 2); $firstname = ucfirst($firstname); $lastname = ucfirst($lastname); echo $firstname . ' ' . $lastname;
The space was indeed the problem, I fixed it by using
$lastname = ucfirst(str_replace(' ', '', $lastname));