As previously mentioned in other questions, with PHP, when attempting to get a substring, it doesn't understand multibyte characters [as you get with UTF8 for example].
What the other answers don't mention is that you should hint the encoding you would like to use for the mb_substr
So, for example, I use this:
mb_substr[ "Sunday", 0, 1,'UTF8']; // Returns S
mb_substr[ "воскресенье", 0, 1,'UTF8']; // Returns в
This is a short guide on how to get the first character of a string in PHP. As an added bonus, I will also show you how to get the first two characters of a string.
A simple code snippet:
length
If
length
is given and is positive, the string returned will contain at mostlength
characters beginning fromoffset
[depending on the length ofstring
].If
length
is given and is negative, then that many characters will be omitted from the end ofstring
[after the start position has been calculated when aoffset
is negative]. Ifoffset
denotes the position of this truncation or beyond, an empty string will be returned.If
length
is given and is0
, an empty string will be returned.If
length
is omitted ornull
, the substring starting fromoffset
until the end of the string will be returned.Example #2 Using a negative
length
Return Values
Returns the extracted part of
string
, or an empty string.Changelog
8.0.0 | length is nullable now. When length is explicitly set to null , the function returns a substring finishing at the end of the string, when it previously returned an empty string.
|
8.0.0 | The function returns an empty string where it previously returned false .
|
Examples
Example #3 Basic substr[] usage
Example #4 substr[] casting behaviour
The above example will output:
1] 'pe' 2] '54' 3] 'gr' 4] '1' 5] '' 6] '' 7] '1200'
Example #5 Invalid Character Range
If an invalid character range is requested, substr[] returns an empty string as of PHP 8.0.0; previously, false
was returned instead.
Output of the above example in PHP 8:
Output of the above example in PHP 7:
See Also
- strrchr[] - Find the last occurrence of a character in a string
- substr_replace[] - Replace text within a portion of a string
- preg_match[] - Perform a regular expression match
- trim[] - Strip whitespace [or other characters] from the beginning and end of a string
- mb_substr[] - Get part of string
- wordwrap[] - Wraps a string to a given number of characters
- String access and modification by character
Andreas Bur [andreas dot buro at gmail dot com] ¶
13 years ago
For getting a substring of UTF-8 characters, I highly recommend mb_substr
biohazard dot ge at gmail dot com ¶
9 years ago
may be by following functions will be easier to extract the needed sub parts from a string:
here comes the source:
bleakwind at msn dot com ¶
17 years ago
This returns the portion of str specified by the start and length parameters..
It can performs multi-byte safe on number of characters. like mb_strcut[] ...
Note:
1.Use it like this bite_str[string str, int start, int length [,byte of on string]];
2.First character's position is 0. Second character position is 1, and so on...
3.$byte is one character length of your encoding, For example: utf-8 is "3", gb2312 and big5 is "2"...you can use the function strlen[] get it...
Enjoy it :] ...
--- Bleakwind
QQ:940641
//www.weaverdream.com
PS:I'm sorry my english is too poor... :[
greg at apparel dot com ¶
8 years ago
Coming to PHP from classic ASP I am used to the Left[] and Right[] functions built into ASP so I did a quick PHPversion. hope these help someone else making the switch
function left[$str, $length] {
return substr[$str, 0, $length];
}
function right[$str, $length] {
return substr[$str, -$length];
}
pugazhenthi k ¶
9 years ago
nikolai dot wuestemann at t-online dot de ¶
11 years ago
If you want to have a string BETWEEN two strings, just use this function:
Petez ¶
15 years ago
I wanted to work out the fastest way to get the first few characters from a string, so I ran the following experiment to compare substr, direct string access and strstr:
[substr] 3.24 [With standard deviations 0.01, 0.02 and 0.04] THEREFORE substr is the fastest of the three methods for getting the first few letters of a string.
The string was 6 paragraphs of Lorem Ipsum, and I was trying match the first two words. The experiment was run 3 times and averaged. The results were:
[direct access] 11.49
[strstr] 4.96
gkhelloworld at gmail dot com ¶
13 years ago
Shortens the filename and its expansion has seen.
kaysar in ymail in com ¶
13 years ago
Drop extensions of a file [even from a file location string]
Hope it may help somebody like me.. [^_^]
output: c:/some dir/abc defg. hi
Anonymous ¶
4 years ago
Be aware of a slight inconsistency between substr and mb_substr
mb_substr["", 4]; returns empty string
substr["", 4]; returns boolean false
tested in PHP 7.1.11 [Fedora 26] and PHP 5.4.16 [CentOS 7.4]
link ¶
13 years ago
I created some functions for entity-safe splitting+lengthcounting:
pheagey at gmail dot com ¶
10 years ago
Using a 0 as the last parameter for substr[].
As per examples
works no problem. However
will get you nothing. Just a quick heads up
Cristianlf ¶
11 years ago
I needed a function like lpad from oracle, or right from SQL
then I use this code :
regards,
Result:
4152
------------------------------------------------
This function is really simple, I just wanted to share, maybe helps someone out there.
fanfatal at fanfatal dot pl ¶
17 years ago
Hmm ... this is a script I wrote, whitch is very similar to substr, but it isn't takes html and bbcode for counting and it takes portion of string and show avoided [html & bbcode] tags too ;]
Specially usefull for show part of serach result included html and bbcode tags
Using this is similar to simple substr.
Greatings ;]
...
fatihmertdogancan at hotmail dot com ¶
8 years ago
[English]
I created python similar accesing list or string with php substr & strrev functions.
Use: str[$string,$pattern]
About the python pattern,
//docs.python.org/release/1.5.1p1/tut/strings.html
//effbot.org/zone/python-list.htm
About of pattern structures
[start:stop:step]
Example,
Output,
thetoacn
eht
aom
htan
This is function phpfiddle link: //phpfiddle.org/main/code/e82-y5d
or source;
Good works..
egingell at sisna dot com ¶
15 years ago