Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php在字符串(Regex)中的大写字母前面放置空格_Php_Regex - Fatal编程技术网

Php在字符串(Regex)中的大写字母前面放置空格

Php在字符串(Regex)中的大写字母前面放置空格,php,regex,Php,Regex,我有一些字符串,其中包含的字是聚在一起,我需要分开他们了 比如说 这很酷-这很酷 我的家在这里 我慢慢地开始熟悉正则表达式,我相信要做到这一点,我应该使用preg_替换。我的问题是将表达式组合在一起以找到匹配项 我只走了这么远 preg_replace('~^[A-Z]~', " ", $string) 每个字符串都包含很多单词,但只有第一个单词包含成束的单词,因此使用我上面的示例,字符串将是 “再次拜访你真酷”-“再次拜访你真酷” 我已经告诉它从一开始就开始,寻找资金,但我不知道怎么做

我有一些字符串,其中包含的字是聚在一起,我需要分开他们了

比如说 这很酷-这很酷
我的家在这里

我慢慢地开始熟悉正则表达式,我相信要做到这一点,我应该使用preg_替换。我的问题是将表达式组合在一起以找到匹配项

我只走了这么远

   preg_replace('~^[A-Z]~', " ", $string)
每个字符串都包含很多单词,但只有第一个单词包含成束的单词,因此使用我上面的示例,字符串将是
“再次拜访你真酷”-“再次拜访你真酷”

我已经告诉它从一开始就开始,寻找资金,但我不知道怎么做 -仅限于每个字符串的第一个字 -如何在空格后替换零件中重复使用大写字母

$string = preg_replace('/[A-Z]/', ' $0', $string);
也许以后再看一遍结果

问题
  • 您的正则表达式将只匹配第一个大写字母。有关更多信息,请查看中的

  • 替换的字符是换行符
    '\n'
    ,而不是空格

  • 解决方案 使用此代码:

    $String = 'ThisWasCool';
    $Words = preg_replace('/(?<!\ )[A-Z]/', ' $0', $String);
    
    $String='ThisWasCool';
    
    $Words=preg_replace('/(?我对正则表达式不太精通,但我建议使用以下代码:

    $string="ThisWasCool to visit you again";
    $temp = explode(' ',$string, 2);
    $temp[0] = preg_replace('/(.)([A-Z])/','$1 $2', $temp[0]);
    $string = join(' ',$temp);
    
    看看SirLancelot代码,我有了第二个解决方案。尽管如此,我还是更喜欢explode解决方案,因为您声明您的目标只是字符串的第一个单词

    $string="ThisWasCool to visit you again";
    $temp = explode(' ',$string, 2);
    $temp[0] = preg_replace('/(?<!^)([A-Z])/',' $0', $temp[0]);
    $string = join(' ',$temp);
    
    $string=“再次访问您真酷”;
    $temp=爆炸(“”,$string,2);
    
    $temp[0]=preg_replace('/(?这是我的.02c版本,此版本将仅作用于第一个单词,并将保留大写字母序列(BMW)


    “\n”用于替换。您是指空格还是换行符?这里太晚了,我是指空格,通常我会将其放在“”中,但这可能是错误的!谢谢我猜/是分隔符,有没有办法将其限制为仅处理第一个字?数据来自csv,因此第一个字被包装在“”中是的,有。我建议使用ltrim删除空格,但您也可以在表达式中使用:/\B[A-Z]/如何跳过第一个大写字母的空格?我得到了:preg_replace('/(?如果您需要不分隔顺序大写字母的空格(例如,BusinessUnitID应该是业务单位ID,而不是业务单位ID),那么您可以使用/(?)?
    $string="ThisWasCool to visit you again";
    $temp = explode(' ',$string, 2);
    $temp[0] = preg_replace('/(?<!^)([A-Z])/',' $0', $temp[0]);
    $string = join(' ',$temp);
    
    $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;