tags:

views:

48

answers:

1

how can I take starting two character of a string regardless the string lenght.

+3  A: 

With substr()

$string = substr("something", 0, 2);
Chacha102
You can also access parts of a string as an array: `$string[0]` and `$string[1]` are its first two characters.
Erik
but only if the original string is two characters or longer. Do this to be safe:$string = strlen($str) > 2 ? substr($str, 0, 2) : $str; (strip only if it's longer than 2 chars)
seanizer
And if you are using UTF8 or other multibyte encoding, use `mb_substr()`
dev-null-dweller