I want to split a word by capital letter in PHP
For example:
$string = "facebookPageUrl";
I want it like this:
$array = array("facebook", "Page", "Url");
How should I do it? I want the shortest and most efficient way.
To split a string on capital letters, call the split() method with the following regular expression - /(? =[A-Z])/ . The regular expression uses a positive lookahead assertion to split the string on each capital letter and returns an array of the substrings.
You can find the Split Columns > By Uppercase to Lowercase option in three places: Home tab—under the Split Column dropdown menu inside the Transform group. Transform tab—under the Split Column dropdown menu inside the Text Column group. Right-click a column—inside the Split Column option.
findall() method to split a string on uppercase letters, e.g. re. findall('[a-zA-Z][^A-Z]*', my_str) . The re. findall() method will split the string on uppercase letters and will return a list containing the results.
In Python, lower() is a built-in method used for string handling. The lower() method returns the lowercased string from the given string. It converts all uppercase characters to lowercase. If no uppercase characters exist, it returns the original string.
You can use preg_split
with the a look-ahead assertion:
preg_split('/(?=\p{Lu})/u', $str)
Here \p{Lu}
is a character class of all Unicode uppercase letters. If you just work with US-ASCII
characters, you could also use [A-Z]
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With