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
  • 39,820
  • 10
  • 58
  • 75
hihihihi
  • 48
  • 1
  • 5
  • 25

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
  • 44,769
  • 7
  • 56
  • 100
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
  • 18,131
  • 10
  • 32
  • 49
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
  • 704,970
  • 168
  • 1,303
  • 1,425
Md Rasel Ahmed
  • 945
  • 8
  • 11