Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Add space before every capital letter

Posted on 2009-07-03
8
Medium Priority
?
788 Views
1 Endorsement
Last Modified: 2016-05-24
I want to add a space between every capital letter.

So this:
           HelloWorldThisIsATest

Should become this:
           Hello World This Is A Test
1
Comment
Question by:hankknight
8 Comments
 
LVL 5

Assisted Solution

by:Opalelement
Opalelement earned 80 total points
ID: 24773463
Snippet:)
<?php
$string = "HelloWorldThisIsATest";
$chars = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY);
foreach($chars as $char)
{
	if(ctype_upper($char))
	{
		$string = str_replace($char, " $char", $string);
	}
}
echo $string;
?>

Open in new window

0
 
LVL 3

Assisted Solution

by:glenthorne
glenthorne earned 80 total points
ID: 24773540
This is an example from:  

http://us2.php.net/manual/en/function.preg-split.php

<?php

function spacify($camel, $glue = ' ') {
    return preg_replace( '/([a-z0-9])([A-Z])/', "$1$glue$2", $camel );
}

echo spacify('CamelCaseWords'), "\n"; // 'Camel Case Words'

?>

The key line if the "preg_replace" command.  The breakdown is as follows:

syntax: preg_replace( mixed $pattern  , mixed $replacement  , mixed $subject  );
In this case:
$pattern =  '/([a-z0-9])([A-Z])/'
$replacement = "$1$glue$2"
$subject = $camel

The trick here is the "pattern" (or regular expression) and the "replacement".  When you put a regular expression, in this case a character list (the list in square brackets []), inside parenthesis (), the "matching" string is stored in $1 ($1 is the for the first set of parenthesis, $2 is for the second set, etc).  So, this regular expression looks for any lower case letters and numbers and stores them in $1, then when it bumps into an upper case letter (stores in $2), the replacement inserts the "glue" (in this case a space) between the two, and the process repeats.
0
 
LVL 18

Expert Comment

by:Hube02
ID: 24773704
Why not just:

$string = 'HelloWorldThisIsATest';
preg_replace('/([A-Z])(?<!^)/', ' $1', $string);
echo $string;

Simply replaces every "cap" with "space cap", except the first character.

([A-Z]) = match any character from A to Z
(?<!^) = negative look behind = Not beginning of string
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:Opalelement
ID: 24773710
Mine makes it so you don't need to understand regex, it is a simple solution that is easy to understand.
0
 
LVL 18

Accepted Solution

by:
Hube02 earned 1840 total points
ID: 24773843
Oops, I forgot a very important assignment. That's what I get for not testing first: See correction below.

As far as to use regular expressions or not, this is up to you. My opinion is that regular expressions are an important part of any programming/scripting language. They allow you to do in a single line what it takes several or many lines to do with simple string manipulation. Most languages worth mentioning use some form of the PERL standard, and usually there is something to tell you how they deviate form this, so learning them they become useful no matter what language your next project is in.


<?php
$string = 'HelloWorldThisIsATest';
$string = preg_replace('/([A-Z])(?<!^)/', ' $1', $string);
echo $string;
?>

Open in new window

0
 
LVL 5

Expert Comment

by:Opalelement
ID: 24773852
I use regular expressions for matching strings but if it is something like this I just use the built in functions.
0
 
LVL 16

Author Comment

by:hankknight
ID: 24773913
Thank you all! Regular expressions rock!
0
 

Expert Comment

by:Ravi Kumar
ID: 41613586
Accepted solution not worked for me, here is my working solution

<?php

    $string = 'HelloWorldThisIsATest';

    $string = preg_replace('/((?<=[a-z])(?=[A-Z])|(?=[A-Z][a-z]))/', ' $1', $string);

    echo $string;
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question