2

I want to explode a string or an integer and separate it by a space.

E.g., I have this int 12345678, and I want its numbers to become like 123 45678. I want the first three numbers separated. Can someone give me a clue or hint in how to achieve this, like what function to use in PHP? I think using an explode will not work here because the explode function needs a separator.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
hihihihi
  • 68
  • 1
  • 5
  • 29

3 Answers3

5

You can use substr_replace() - Replace text within a portion of a string.

echo substr_replace(1234567, " ", 3, 0); // 123 4567

https://3v4l.org/9CFlX

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
4

You could use substr() :

$str = "12345678" ;
echo substr($str,0,3)." ".substr($str, 3); // "123 45678"

Also works with an integer :

$int = 12345678 ;
echo substr($int,0,3)." ".substr($int, 3); // "123 45678"
Syscall
  • 19,327
  • 10
  • 37
  • 52
0

This problem will solve by using substr().

The substr() function returns a part of a string.

Syntax: substr(string,start,length)

Example:

$value = "12345678";
echo substr($value,0,3)." ".substr($value, 3);

Output: 123 45678

You may get better understand from here.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Md Rasel Ahmed
  • 1,025
  • 9
  • 12