将标题大小写应用于字符串中的所有单词(指定缩写词除外)

发布于 2024-09-10 12:56:22 字数 422 浏览 5 评论 0原文

示例输入: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南城追梦 2024-09-17 12:56:22

据我了解,您希望所有学校名称的每个单词的第一个字符都是大写,并在此处理中排除一些特殊单词(我的示例中的 $exceptions)。

您可以这样做:

   function createSchoolName($school) {
      $exceptions = array('SMK', 'PTS', 'SBP');
      $result = "";
      $words = explode(" ", $school);
      foreach ($words as $word) {
          if (in_array($word, $exceptions))
              $result .= " ".$word;
          else
              $result .= " ".strtolower($word);
      }
      return trim(ucwords($result));
   }

echo createSchoolName('SMK SUNGAI PUNAI');

此示例将根据您的问题的要求返回 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:

   function createSchoolName($school) {
      $exceptions = array('SMK', 'PTS', 'SBP');
      $result = "";
      $words = explode(" ", $school);
      foreach ($words as $word) {
          if (in_array($word, $exceptions))
              $result .= " ".$word;
          else
              $result .= " ".strtolower($word);
      }
      return trim(ucwords($result));
   }

echo createSchoolName('SMK SUNGAI PUNAI');

This example would return SMK Sungai Punai as required by your question.

寄意 2024-09-17 12:56:22

您可以非常简单地创建一组用竖线分隔的排除单词/首字母缩略词,然后使用 (*SKIP)(*FAIL) 来防止匹配这些整个单词。

mb_convert_case() 是一个非常好的调用函数,因为它可以立即提供 TitleCasing 并且是多字节安全的。

代码:(演示

$pipedExclusions = 'SMK|USA|AUS';
echo preg_replace_callback(
         '~\b(?:(?:' . $pipedExclusions . ')(*SKIP)(*FAIL)|\p{Lu}+)\b~u',
         fn($m) => mb_convert_case($m[0], MB_CASE_TITLE),
         'SMK SUNGAI PUNAI'
     );
// SMK Sungai Punai

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)

$pipedExclusions = 'SMK|USA|AUS';
echo preg_replace_callback(
         '~\b(?:(?:' . $pipedExclusions . ')(*SKIP)(*FAIL)|\p{Lu}+)\b~u',
         fn($m) => mb_convert_case($m[0], MB_CASE_TITLE),
         'SMK SUNGAI PUNAI'
     );
// SMK Sungai Punai
固执像三岁 2024-09-17 12:56:22

确实没有什么好的办法。在这种情况下,您可以假设它是缩写,因为它只有三个字母长并且不包含元音。您可以编写一组规则来查找字符串中的缩写,然后将它们大写,但在某些情况下这是不可能的......考虑“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."

喜你已久 2024-09-17 12:56:22

你可以使用这样的东西:

<?php
$str = 'SMK SUNGAI PUNAI';
$str = strtolower($str);
$arr = explode(" ", $str);

$new_str = strtoupper($arr[0]). ' ' .ucfirst($arr[1]). ' ' .ucfirst($arr[2]);
echo '<p>'.$new_str.'</p>';

// Result: SMK Sungai Punai
?>

You can use something like this:

<?php
$str = 'SMK SUNGAI PUNAI';
$str = strtolower($str);
$arr = explode(" ", $str);

$new_str = strtoupper($arr[0]). ' ' .ucfirst($arr[1]). ' ' .ucfirst($arr[2]);
echo '<p>'.$new_str.'</p>';

// Result: SMK Sungai Punai
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文