Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explode UTF8 string regarding to uppercase or numeric characters

As this question, I can split strings that includes upper cases like this:

function splitAtUpperCase($string){
    return preg_replace('/([a-z0-9])?([A-Z])/','$1 $2',$string);
}

$string = 'setIfUnmodifiedSince';
echo splitAtUpperCase($string);

Output is "set If Unmodified Since"

But I need some modification:

  • That code snippet doesn't handle the cases, when these characters exist in string: ÇÖĞŞÜİ. I don't want to transliterate the characters. Then I lose meaning of word. I need to use some UTF characters. That code makes "HereÇonThen" to "HereÇon Then"
  • I also don't want to split uppercase abbreviations. If word is "IKnowYouWillComeASAPHere" I need it to be converted to "I Know You Will Come ASAP Here"
  • Don't explode if all letters are uppercase. Like "DONTCOMEHERE"
  • Explode also numeric values. "Before2013ends" to "Before 2013 ends"
  • Explode if first character is hash key (#).

cases and expected results

  1. "comeHEREtomorrow" => "come HERE tomorrow"
  2. "KissYouTODAY" => "kiss you TODAY"
  3. "comeÜndeHere" => "come Ünde Here"
  4. "NEVERSAYIT" => "NEVERSAYIT"
  5. "2013willCome" => "2013 will Come"
  6. "Before2013ends" => "Before 2013 ends"
  7. "IKnowThat" => "I Know That"
  8. "#whatiknow" => "# whatiknow"

For these cases I use subsequent str_replace operations. I look for a short solution that doesn't make too much for loops to check the words. It would be better to have it as preg_replace or etc. if possible.

Edit: Anyone can try his solution by changing convert function inside this PHP fiddle: http://ideone.com/9gajZ8

like image 715
trante Avatar asked Jan 08 '13 11:01

trante


1 Answers

/([[:lower:][:digit:]])?([[:upper:]]+)/u should do it.

Here /u is used for Unicode characters. and ([[:upper:]]+) is used for Sequence of upper cased letters.

Note. Case of a letter depends on the character set you are using.

like image 190
Shiplu Mokaddim Avatar answered Sep 22 '22 15:09

Shiplu Mokaddim