Space on capital letter
Moderator: General Moderators
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
Space on capital letter
Alright so I have a string like this: 'SavedSearches' and I want to turn it into 'Saved Searches' (notice the space). How do I do that? I don't even know where to begin.
Something like this..
Untested.
Edit: Added trim after looking at the code again because it will put a space in front of the first character..
Untested.
Code: Select all
$length = strlen($String);
$newString = null;
for ($i = 0; $i < $length; $i++) {
if ((chr($String{$i}) > 64) && (chr($String{$i}) < 91)) {
$newString .= ' ' . $String{$i};
} else {
$newString .= $String{$i};
}
}
$newString = trim($newString);- MarK (CZ)
- Forum Contributor
- Posts: 239
- Joined: Tue Apr 13, 2004 12:51 am
- Location: Prague (CZ) / Vienna (A)
- Contact:
Code: Select all
<?php
$strings = array("SavedSearches",
"devNetworkHomepage");
forEach ($strings as $item)
echo ereg_replace("([a-z])([A-Z])", "\\1 \\2", $item)."<br />\r\n";
?>php output wrote:Saved Searches<br />
dev Network Homepage<br />
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
I've been using this for a bit of time. It not only breaks apart strings like requested, but also nicely breaks out capped words (a.k.a. acronymns and such.)
Code: Select all
$name = preg_split('#([^A-Za-z]+|[A-Z]?[a-z]+)#', $name, -1, PREG_SPLIT_DELIM_CAPTURE);
$name = array_map('trim', $name);
$name = array_filter($name);
$name = implode(' ', $name);Keeping it simple... Just use a zero width assertion and insert a space...
Code: Select all
$result = preg_replace('/(?=(?<=[a-z])[A-Z])/', ' ', $input);