-2

My String is like this

$str="12345"

I want to get

$str1 = "1,2,3,4,5"
LF00
  • 27,015
  • 29
  • 156
  • 295

4 Answers4

1

explose to an array with str_split and then implode

implode(str_split('testing'), ',')

will give you

t,e,s,t,i,n,g

samefor 1234

Ôrel
  • 7,044
  • 3
  • 27
  • 46
  • 1
    you wrongly use implode, here is the [manual implode](http://php.net/manual/en/function.implode.php) – LF00 May 24 '17 at 03:42
  • 1
    No both order are acceptable – Ôrel May 24 '17 at 04:08
  • putting array first hits the first `if` see source code, you win a comparison : https://github.com/php/php-src/blob/dd4a78b631b356beb8c7b5ab7f58a91008143d5a/ext/standard/string.c#L1308 – Ôrel May 24 '17 at 04:14
  • Yeah, I see it. you're right. – LF00 May 24 '17 at 04:17
0

try this, check the live demo

implode(',', str_split($str));
LF00
  • 27,015
  • 29
  • 156
  • 295
0
$strLen = strlen($s);
$o = '';
for($i =0; $i < $strLen; $i++) {
      $comma = ($i < $strLen) ? ' , ' : '';
      $o .= $strLen[$i] . $comma;
 }
 echo $o;
Shubhranshu
  • 511
  • 3
  • 12
0

Without explode or implode:

$str="123456789";
echo substr(chunk_split($str,1,","),0,-1);
Pang
  • 9,564
  • 146
  • 81
  • 122
sumit
  • 15,003
  • 12
  • 69
  • 110