一尘不染

使用php preg_match(正则表达式)将camelCase单词拆分为单词

php

我将如何拆分单词:

oneTwoThreeFour

放入数组,这样我就可以得到:

one Two Three Four

preg_match

我很累,但这只是整个词

$words = preg_match("/[a-zA-Z]*(?:[a-z][a-zA-Z]*[A-Z]|[A-Z][a-zA-Z]*[a-z])[a-zA-Z]*\b/", $string, $matches)`;

阅读 380

收藏
2020-05-29

共1个答案

一尘不染

您还可以preg_match_all用作:

preg_match_all('/((?:^|[A-Z])[a-z]+)/',$str,$matches);

说明:

(        - Start of capturing parenthesis.
 (?:     - Start of non-capturing parenthesis.
  ^      - Start anchor.
  |      - Alternation.
  [A-Z]  - Any one capital letter.
 )       - End of non-capturing parenthesis.
 [a-z]+  - one ore more lowercase letter.
)        - End of capturing parenthesis.
2020-05-29