Breaking up strings and adding character inbetween.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Breaking up strings and adding character inbetween.

Post by anthony88guy »

Okay, Im trying to make a small function that would take a string such as $foo = "string"; and convert that to $foo = "s+t+r+i+n+g"; Basily breakdown the string and put it together adding a character inbetween each character of the string.

Anyideas?

Thanks
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

Code: Select all

function strBreak($string, $separator)
{
  $length = strlen($string);
  $letters = array();
  for ($i = 0; $i < $length; $i++)
  {
    $letter = substr($string, $i, 1);
    array_push($letters, $letter);
  }
  return implode($separator, $letters);
}

echo strBreak('string', '+');
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

works great, I was thinking about using preg_split.

Thanks
Post Reply