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
Solved

Add space before every capital letter

Posted on 2009-07-03
8
731 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
The viewer will learn how to count occurrences of each item in an array.
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.

856 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