Chathuri Fernando
Chathuri Fernando

Reputation: 972

String spliting in a word (PHP)

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?

Upvotes: 0

Views: 57

Answers (3)

Bilal Ahmed
Bilal Ahmed

Reputation: 4066

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

Upvotes: 2

L McClean
L McClean

Reputation: 350

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
)

Upvotes: 1

Kerberos
Kerberos

Reputation: 4166

You can add this to your code:

substr($word, 4);

Upvotes: 1

Related Questions