0

I split a string until the first four characters into an array with this code:

$word = "google";    
substr($word, 0, 4);

But now I want to get the rest of the string into another array.

Ex: here I want to get "le" into another array. Is there a way to do this? Can anyone help me?

Kerberos
  • 4,036
  • 3
  • 36
  • 55
Chathuri Fernando
  • 950
  • 3
  • 11
  • 22
  • 2
    Possible duplicate of [Splitting strings in PHP and get last part](https://stackoverflow.com/questions/17030779/splitting-strings-in-php-and-get-last-part) – Chris Satchell Aug 10 '18 at 10:32

3 Answers3

2

You should use str_split function like:

$string = 'google';
$output = str_split($string, 4);
print_r($output);

you can check your desired output here

for more details about str_split

Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42
1

To get the rest of the string - you can use the substr function with a single argument.

$word = "google";
$start = substr($word, 0, 4);
$rest = substr($word, 4);

If you want to actually create an array of characters from a string, there is a str_split function:

e.g

$input = "google";
$result = str_split($input); 
print_r($result);

// Which would print:
Array
(
    [0] => G
    [1] => o
    [2] => o
    [3] => g
    [4] => l
    [5] => e
)
L McClean
  • 340
  • 1
  • 8
1

You can add this to your code:

substr($word, 4);
Kerberos
  • 4,036
  • 3
  • 36
  • 55