Solved

Add space before every capital letter

Posted on 2009-07-03
8
714 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 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now