Solved

Add space before every capital letter

Posted on 2009-07-03
8
745 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 5

Assisted Solution

by:Opalelement
Opalelement earned 20 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 20 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
Industry Leaders: 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 460 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

724 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