0
What is the correct syntax for selecting the first letter in a string? In php
I've tried [0] but have been told it's wrong?
3 Antworten
+ 4
@david here you can try this.
https://code.sololearn.com/wbwby7HzaZy0/?ref=app
+ 2
have you tried for loop?
+ 1
$myString = "Hello";
$first = $myString[0]; // This actually gets the first Byte not necessarily the character
$first = subset($myString, 0, 1) // Again gets the first Byte
These may have issues when the the encoding character type, like UTF-8, is multibyte.
So use:
$first = mb_substr($myString, 0, 1, 'utf-8');
http://php.net/manual/en/function.mb-substr.php