php - 无法让 ucfirst() 处理变量
问题描述
我制作了一个代码,它从电子邮件中获取名字和姓氏,$firstname 是大写的,但 $lastname 不是。为什么?
<html>
<body>
<?php
$email = "test.testt@testing.com";
$firstname = ucfirst(strtok(strtok($email, "@"), "."));
$lastname = substr(strtok(strtok($email, "@"), ".") . ' ' . strtok("."), strrpos(strtok(strtok($email, "@"), ".") . ' ' . strtok("."), ' '));
$lastname = ucfirst($lastname);
echo $firstname.$lastname;
?>
</body>
</html>
输出:测试testt
任何帮助将不胜感激
解决方案
除了空间的实际问题外,您的代码还进行了大量搜索和切分字符串。
您可以先使用explode()
with@
再使用 a来简化它.
。然后ucfirst
在最后一次操作的每个部分上使用......
$names = explode("@", $email);
// Get first 2 parts of name and split it by the .
[$firstname, $lastname] = explode(".", $names[0], 2);
$firstname = ucfirst($firstname);
$lastname = ucfirst($lastname);
echo $firstname . ' ' . $lastname;
推荐阅读
- sql-server - 从 Azure Web 应用程序连接到内部 SQL 数据库
- python - 熊猫选择包含两个值的行(包括)
- sas - SAS 企业指南中回归中的 0 DF
- r - 将列表中的小标题加入一个小标题
- rust - 如何使用 Serde 反序列化 park_lot::Mutex?
- git - Git 说“1 个文件已更改,0 个插入(+),0 个删除(-)”
- jboss - 集群模式下 JBoss EAP7.1 集成 ActiveMQ Artemis 消息重新分发不起作用
- google-sheets - 在前 10 名的同一工作表中创建链接
- sublimetext3 - 如何在 Sublime Text 3 中恢复调色板
- python - 自定义周计算,其中周从熊猫的星期四晚上 7 点开始;