Php 在字符串中的大写字母前放置一个空格(正则表达式)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1089613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:55:01  来源:igfitidea点击:

Php put a space in front of capitals in a string (Regex)

phpregex

提问by Paul M

I have a number of strings which contain words which are bunched together and I need to seperate them up.

我有许多字符串,其中包含聚集在一起的单词,我需要将它们分开。

For example ThisWasCool - This Was Cool
MyHomeIsHere - My Home Is Here

例如 ThisWasCool - 这很酷
MyHomeIsHere - 我的家在这里

Im slowly getting my head around regular expressions and I believe to do this I should use preg_replace. My problem is putting together the expression to find the match.

我慢慢地了解正则表达式,我相信要做到这一点,我应该使用 preg_replace。我的问题是将表达式放在一起以找到匹配项。

I have only got this far

我只有这一步

   preg_replace('~^[A-Z]~', " ", $string)

Each string contains a lot of words, but ONLY the first word contains bunched words so using my example above a string would be
"ThisWasCool to visit you again" - "This Was Cool to visit you again"

每个字符串包含很多单词,但只有第一个单词包含成束的单词,因此使用我上面的示例,字符串将是
“ThisWasCool to visit you again” - “This Was Cool to visit you again”

I have told it to start at the beginning, and look for capitals, but what I dont know how to do is - restrict it only to the first word of each string - how to reuse the capital letter in the replace part after the space

我已经告诉它从头开始,并寻找大写字母,但我不知道该怎么做 - 将其限制为每个字符串的第一个单词 - 如何在空格后的替换部分重用大写字母

回答by matpie

Problem

问题

  1. Your regex '~^[A-Z]~'will match only the first capital letter. Check out Meta Charactersin the Pattern Syntaxfor more information.

  2. Your replacement is a newline character '\n'and not a space.

  1. 您的正则表达式'~^[A-Z]~'将仅匹配第一个大写字母。查看模式语法中的元字符以获取更多信息。

  2. 您的替换是换行符'\n'而不是空格。

Solution

解决方案

Use this code:

使用此代码:

$String = 'ThisWasCool';
$Words = preg_replace('/(?<!\ )[A-Z]/', ' 
$string = preg_replace('/[A-Z]/', ' 
$string = ltrim(preg_replace('/[A-Z]/', ' 
$str = "CheckOutMyBMW I bought it yesterday";
$parts = explode(' ', $str);
$parts[0] = preg_replace('~([a-z])([A-Z])~', '\1 \2', $parts[0]);
$newstr = implode(' ', $parts);
echo $newstr;
', $string));
', $string);
', $String);

The (?<!\ )is an assertionthat will make sure we don't add a space before a capital letter that already has a space before it.

(?<!\ )是一个断言,它将确保我们不会在已经有空格的大写字母之前添加空格。

回答by Alex Barrett

$string="ThisWasCool to visit you again";
$temp = explode(' ',$string, 2);
$temp[0] = preg_replace('/(.)([A-Z])/',' ', $temp[0]);
$string = join(' ',$temp);

Maybe run the result through ltrimafter.

也许之后通过ltrim运行结果。

$string="ThisWasCool to visit you again";
$temp = explode(' ',$string, 2);
$temp[0] = preg_replace('/(?<!^)([A-Z])/',' ##代码##', $temp[0]);
$string = join(' ',$temp);

回答by too much php

Here's my .02c, this version will only act on the first word, and will preserve sequences of uppercase letters (BMW).

这是我的 .02c,这个版本只会作用于第一个单词,并且会保留大写字母序列 (BMW)。

##代码##

回答by Eineki

I'm not proficient with regular expression but I would suggest something like the following code:

我不精通正则表达式,但我建议使用以下代码:

##代码##

Looking at SirLancelot code I've got a second solution. Still I prefer the explode solution as you stated that your target it is only the first word of the string.

查看 SirLancelot 代码我有第二个解决方案。我仍然更喜欢爆炸解决方案,因为您说您的目标只是字符串的第一个单词。

##代码##