0

I have this code:

$text = "12345678910";
$codebits = str_split($text ,3);
for($b = 0; $b<3; $b++){
  echo "$codebits[$b]yes";
}

which outputs the result:

123yes456yes789yes

How can I change it to be in one line? For example:

123456789yes
DaveyDaveDave
  • 9,821
  • 11
  • 64
  • 77
Yung Drael
  • 21
  • 6
  • 1
    `for($b = 0; $b<3; $b++){ echo "$codebits[$b]"; } echo "yes";` – splash58 Nov 09 '17 at 07:46
  • Can you describe more generally what you need the code to do? For this example, it looks like you're saying you want to go from `1234567890` to `123456789yes`. So, do you mean you want to swap the `0` for `yes`? Or the last character? Or the tenth character? Maybe give a few more examples of input and expected output? – DaveyDaveDave Nov 09 '17 at 07:50
  • you don't need `str_split` if you want to just replace the last 2 chars with `yes` – RomanPerekhrest Nov 09 '17 at 07:54
  • Using `str_split` is absolute nonsense for this task. Who upvoted this question? It is not well researched, and I'm hoping it is not clear so that the question is updated to better explain the task. I can't imagine this question will help any future SO readers. – mickmackusa Nov 09 '17 at 08:10
  • Possible duplicate of [Limit String Length](https://stackoverflow.com/questions/3019285/limit-string-length) and https://stackoverflow.com/questions/6332305/limit-number-of-characters-and-put-read-more-link and many, many more because this is a RTM question. – mickmackusa Nov 09 '17 at 22:26

2 Answers2

0

Use substr() like below:-

<?php

echo substr($text,0,8)."yes";

Output:-https://eval.in/896033

Or if you want to use your one:-

<?php

$text = "12345678910";
$codebits = str_split($text ,3);
for($b = 0; $b<3; $b++){
  echo "$codebits[$b]";
}
echo "yes";

Output:- https://eval.in/896040

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
-1

if you must use str_split:

<?php
$text = "12345678910";
$codebits = str_split($text ,strlen($text)-2);
$codebits[1]="yes";
for($b = 0; $b<2; $b++){
  echo "$codebits[$b]";
}
?>

And you can concatanate array $codebits:

$fullstring=$codebits[0].$codebits[1];
echo $fullstring;
OulinaArt
  • 324
  • 4
  • 13