Home  >  Article  >  Backend Development  >  How to Split CamelCase Words Using preg_match in PHP?

How to Split CamelCase Words Using preg_match in PHP?

Linda Hamilton
Linda HamiltonOriginal
2024-10-24 10:31:01975browse

How to Split CamelCase Words Using preg_match in PHP?

Splitting CamelCase Words with preg_match

Question:

How can I split a camelCase word like "oneTwoThreeFour" into individual words such as "one Two Three Four" using preg_match in PHP?

Answer:

Instead of using preg_match, you can employ the preg_split function as follows:

<code class="php">$arr = preg_split('/(?=[A-Z])/', $str);</code>

Explanation:

Preg_split() with the regular expression "/(?=[A-Z])/" splits the input string just before any uppercase letter encountered. This behavior is achieved because the regular expression matches the position just before an uppercase letter, enabling you to break down the camelCase word into its component words.

The above is the detailed content of How to Split CamelCase Words Using preg_match in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn