?
Solved

Add space before every capital letter

Posted on 2009-07-03
8
Medium Priority
?
765 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 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
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
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

7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

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…
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.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

765 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