简体   繁体   English

使用PHP / jQuery从mysql中提取每个单词的首字母大写

[英]Capitalize first letter of each word pulled from mysql with PHP/jQuery

I've stored Names all lower case into a mysql db but my users want to see them on the website as first letters capitalized. 我已经将Names所有小写字母存储到mysql db中,但是我的用户希望将网站上的首字母大写。 however i'm trying to figure out a way, whether server-side(PHP) or client-side (jQuery), to do this without changing anything in the database? 但是我试图找出一种方法,无论是服务器端(PHP)还是客户端(jQuery),都无需更改数据库中的任何内容? is this possible? 这可能吗?

CODE: 码:

$result = mysql_query("SELECT user from `users` order by user asc") or die(mysql_error());
            echo '<select name="user" class="user">';
            while ($row = mysql_fetch_array($result)) {
                echo "<option value='".$row['user']."'>".$row['user']."</option>";
            }
            echo '</select>';

there is also 也有

ucwords($row['user']);

if you want to capitalize all the words. 如果您想将所有单词都大写。

You can also do it with CSS: 您也可以使用CSS来做到这一点:

#example {
  text-transform: capitalize;
}

PHP has the ucfirst function. PHP具有ucfirst函数。

echo "<option value='".$row['user']."'>".ucfirst($row['user'])."</option>";

By only changing the value inside the option , rather than the value attribute, you can ensure that the same value will be sent back to the server. 通过仅更改option内部的值,而不是value属性,可以确保将相同的值发送回服务器。 Note that you could also use strtolower to ensure that the string is all lower case. 请注意,您也可以使用strtolower来确保字符串全部为小写。

in PHP you can use the function UCfirst(str); 在PHP中,您可以使用函数UCfirst(str);。 to achieve this. 为达到这个。

You could also do this in your SQL query - but not sure this is best way of doing it. 您也可以在SQL查询中执行此操作-但不确定这是否是最佳方法。

SELECT CONCAT(UPPER(SUBSTRING(firstName, 1, 1)), 
LOWER(SUBSTRING(firstName FROM 2))) AS properFirstName

Or you can do it via css like this: 或者您可以通过CSS来做到这一点:

div.caps { text-transform: capitalize; } 

Result is: 结果是:

This Is Some Text. 这是一些文本。

您也可以使用MySQL来做到这一点:

SELECT CONCAT(UPPER(SUBSTR(user, 1, 1)), SUBSTR(user, 2)) FROM `users` ORDER BY user ASC

something like this 像这样的东西

$result = mysql_query("SELECT user from `users` order by user asc") or die(mysql_error());
echo '<select name="user" class="user">';
while ($row = mysql_fetch_array($result)) {
    $user = explode(" ", $row['user']);
    echo "<option value='".$row['user']."'>".ucfirst($user[0]).' '.ucfirst($user[1])."</option>";
}
echo '</select>';

Note that the mapping between upper and lower case letters is not the same in every country; 请注意,在每个国家/地区,大写字母和小写字母之间的映射都不相同; the most notable example being Turkey, where i <-> İ and ı <-> I; 最著名的例子是土耳其,其中我<->İ和ı<-> I; also, names tend to have more complex capitalisation rules (eg "MacDonald", "deVries", "von Hohenzollern" are all proper family names) 同样,名字往往具有更复杂的大写规则(例如“ MacDonald”,“ deVries”,“ von Hohenzollern”都是正确的姓氏)

I'd run the conversion over the database once, and leave the presentation layer alone. 我曾经在数据库上运行过一次转换,而未使用表示层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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