Jump to content

Recommended Posts

$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;

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;
}

 

Not tested, off the top of my head.

 

EDIT: Changed || to &&

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?

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

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

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

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!!!

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.