[wd_asp elements=’search’ ratio=’100%’ id=1]

Take off / Get first 5 characters (substr)

10th February 2014

Php - Strings

php codehaven

This gets the first 5 characters of a string
‘Monday’ Becomes ‘Monda’
and ‘saturday’ Becomes ‘satur’

For single-byte strings (e.g. US-ASCII, ISO 8859 family, etc.) use substr and for multi-byte strings (e.g. UTF-8, UTF-16, etc.) use mb_substr:

// singlebyte strings
$result = substr($myStr, 0, 5);
// multibyte strings
$result = mb_substr($myStr, 0, 5);

Below starts from the third character

$divid = "dog12345";
echo $divid = substr($divid, 3);

The above result would be 12345