Search code examples
phparraysstringseparator

Store string into array from length of character


What i actually want to do is:

i.e

$str="abcdefghijklmnopqrst";

Now i want to store characters from string which are strats from number 2 to 5.

I have try following :-

<?php
$string_name='abcdefghijklmnopqrst';
print_r(str_split($string_name));
echo '<br>';
print_r(str_split($string_name,4));
echo '<br>';
?>

but this will make array with 4 characters. And i have no separator so i have only way to separate word is from length.


Solution

  • you can use substr

    $str = 'abcdefghijklmnopqrstuvwxyz';
    echo substr($str, 1, 4); // where 1 starts b and ends at f