简体   繁体   中英

PHP: Make first letter capital in list?

As i have this:

$full_name = $data['full_name'];
list($firstname, $lastname) = explode(' ', $_POST['full_name']);

(after this comes a query where it inserts $lastname and $firstname )

Where should i use ucfirst() ?

Should i just make new variables again?

$newfirstname = ucfirst($firstname);
$newlastname = ucfirst($lastname);

or can i integrate ucfirst somehow somewhere in the code at top?

list($firstName, $lastName) = array_map('ucfirst', explode(' ', $_POST['full_name']));

array_map将函数应用于所有数组元素。

If they are proper names you could simply use the ucwords() function on the string.

http://www.php.net/manual/en/function.ucwords.php

I suggest using something like

$full_name = ucwords(strtolower($_POST['full_name']));

your approach

list($firstname, $lastname) = explode(' ', $_POST['full_name']);

doesn't work for names containing more than one space.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM