将标题大小写应用于字符串中的所有单词(指定缩写词除外)
示例输入:SMK SUNGAI PUNAI
我的代码:
$school = 'SMK SUNGAI PUNAI';
echo ucwords(strtolower($school));
不需要的输出:Smk Sungai Punai
问题
如何生成输出SMK Sungai Punai 允许
SMK
保留全部大写。
更新。
问题 我有 10,000 个学校名称的列表。我从 PDF 转换为 mysql。我从 PDF 中准确复制了学校名称 - 全部大写。
如何实现有条件的标题大小写?
Example Input: SMK SUNGAI PUNAI
My Code:
$school = 'SMK SUNGAI PUNAI';
echo ucwords(strtolower($school));
Unwanted Output: Smk Sungai Punai
Question
How to make the output SMK Sungai Punai
which allows SMK
to remain in ALL-CAPS.
Update.
The problem I have list of 10,000 school names. From PDF, I convert to mysql. I copied exactly from PDF the name of schools -- all in uppercase.
How can I implement conditional title-casing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
据我了解,您希望所有学校名称的每个单词的第一个字符都是大写,并在此处理中排除一些特殊单词(我的示例中的 $exceptions)。
您可以这样做:
此示例将根据您的问题的要求返回
SMK Sungai Punai
。As far as I understand you want to have all school names with the first character of every word in uppercase and exclude some special words ($exceptions in my sample) from this processing.
You could do that like this:
This example would return
SMK Sungai Punai
as required by your question.您可以非常简单地创建一组用竖线分隔的排除单词/首字母缩略词,然后使用
(*SKIP)(*FAIL)
来防止匹配这些整个单词。mb_convert_case()
是一个非常好的调用函数,因为它可以立即提供 TitleCasing 并且是多字节安全的。代码:(演示)
You can very simply create a pipe-delimited set of excluded words/acronyms, then use
(*SKIP)(*FAIL)
to prevent matching those whole words.mb_convert_case()
is an excellent function to call because it instantly provides TitleCasing and it is multibyte safe.Code: (Demo)
确实没有什么好的办法。在这种情况下,您可以假设它是缩写,因为它只有三个字母长并且不包含元音。您可以编写一组规则来查找字符串中的缩写,然后将它们大写,但在某些情况下这是不可能的......考虑“BOB PLAYS TOO MUCH WOW”。
There's no really good way to do it. In this case you can assume it's an abbreviation because it's only three letters long and contains no vowels. You can write a set of rules that look for abbreviations in the string and then uppercase them, but in some cases it'll be impossible... consider "BOB PLAYS TOO MUCH WOW."
你可以使用这样的东西:
You can use something like this: