Jump to content

[SOLVED] Split Name by Captial Letters


jesushax

Recommended Posts

hi say i have a value

 

"JohnSmith"

 

would it e possible to change with code to

 

"John Smith"

 

there needs to be no sapces in teh db field name as its used for other scripts

 

but i would like to display it on pages with a space

 

THanks

Link to comment
Share on other sites

$string = "JohnSmith";

$splitStringArray = preg_split('/([A-Z])/',$string,-1,PREG_SPLIT_NO_EMPTY+PREG_SPLIT_DELIM_CAPTURE);

$splitString = '';
for($i = 0; $i < count($splitStringArray); $i++) {
if (($i > 0) && (($i % 2) == 0)) $splitString .= ' ';
$splitString .= $splitStringArray[$i];
}

echo $splitString;

Link to comment
Share on other sites

I just tested it and it works.

 

<?php
echo addSpaces('JoeBloggs');

function addSpaces($str) {
  $tmp='';
  $len=strlen($str);
  for ($i=0;$i<$len;++$i) {
    $tmp.=($str[$i]>='A'&&$str[$i]<='Z'&&$i>0 ? ' ' : '').$str[$i];
  }
  return $tmp;
}
?>

 

In my browser I got

Joe Bloggs

 

Did you see the edit I made after posting?

Link to comment
Share on other sites

that didnt work :|

 

in dreamweaver PREG_SPLIT_NO_EMPTY+PREG_SPLIT_DELIM_CAPTURE);

 

didnt show up as an colour could something be wrong there?

Did you actually try running it, or simply take dreamweaver's non-higjlighting as a sign that it wouldn't work

Link to comment
Share on other sites

yes i tested it regardless of dreamweavers non colouring, im not stupid lol

 

ah yes it did work :)

 

i had my functions.php include lower then the text i wanted to run the addSpaces function on

moved it to the top and its all good now

 

thankyou very much for your time

Link to comment
Share on other sites

yes i tested it regardless of dreamweavers non colouring, im not stupid lol

 

ah yes it did work :)

In that case, be aware that it won't work with single letter words such as the A in Yesideez's example

JoeBloggsLikesToEatAPlatterOfFish

I think you'll find I tested it and it worked!!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.