Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split word by capital letter

Tags:

php

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.

like image 596
John Smith Avatar asked Mar 10 '12 09:03

John Smith


People also ask

How do you split a capital letter?

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.

How do I split text in Excel with capital letters?

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.

How do you split a word with a capital letter in Python?

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.

How do you separate lowercase and uppercase in Python?

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.


1 Answers

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.

like image 188
Gumbo Avatar answered Sep 23 '22 18:09

Gumbo