You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP按大写字母拆分字符串但保留TLA的完整解决方案

Fixing TLA Splitting in PHP Camel Case to Space-Separated Strings

Got it, let's tackle that annoying issue where trailing TLAs (like USA at the end of your string) get incorrectly split into US A. Your original regex was close, but it didn't account for sequences of uppercase letters that sit at the end of the string—let's fix that.

The Problem with the Original Regex

Your original pattern /(?<!^)((?![[:upper:]])[[:upper:]]|[[:upper:]](?![[:upper:]]))/ matches any single uppercase letter that either has no uppercase before it or no uppercase after it. That works for most cases, but when you have a trailing TLA (e.g., USA), the last A has no uppercase after it, so the regex matches it and inserts a space before, splitting USA into US A.

The Solution

We need to adjust the regex to only insert spaces in valid positions that respect TLAs (sequences of 2+ uppercase letters), whether they're in the middle or at the end of the string. Here's the improved code:

$string = 'TodayILiveInTheUSAWithSimonUSA';
// Improved regex to handle TLAs (including trailing ones)
$regex = '/(?<!^)(?<![A-Z])([A-Z])|(?<=[A-Z])([A-Z](?![A-Z]))/';
$formattedString = preg_replace($regex, ' $1$2', $string);

echo $formattedString;
// Output: Today I Live In The USA With Simon USA

How This Regex Works

Let's break down the pattern to understand why it fixes the issue:

  • (?<!^)(?<![A-Z])([A-Z]): Matches an uppercase letter that isn't at the start of the string and isn't preceded by another uppercase letter. This handles cases where a lowercase letter is followed by an uppercase letter (e.g., TodayI → Today I, TheUSA → The USA).
  • (?<=[A-Z])([A-Z](?![A-Z])): Matches an uppercase letter that is preceded by another uppercase letter but isn't followed by one. This handles cases where a TLA is followed by a regular word (e.g., USAWith → USA With).

By combining these two cases, we ensure:

  • TLAs (like USA) stay intact, even when they're at the end of the string
  • Regular camel-case word breaks still work (e.g., ILive → I Live, WithSimon → With Simon)

Test Cases to Verify

Let's check a few edge cases to make sure it works:

  • Input: ILoveMYSQLAndPHP → Output: I Love MYSQL And PHP
  • Input: NASAIsCoolNASA → Output: NASA Is Cool NASA
  • Input: ThisIsATestTLA → Output: This Is A Test TLA

All of these handle TLAs in the middle and at the end correctly, no unwanted splits.

内容的提问来源于stack exchange,提问作者kusflo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:10:51