First, the preface
select()
when you read a one-to-many amount of information from a database, you often need to split the retrieved array into a specific character and then concatenate it into a string.
Common syntax formats:
foreach ($arr as $key => $value) {
$arr _str = $arr [' x_id ']. ',' . $arr _str;
}
Suppose the characters $arr
in a character array are
Arr[0] = ' a ';
Arr[1] = ' B ';
ARR[2] = ' C ';
Then, after the concatenation of the $arr_str
string for a,b,c, this time, we need to the last character ', ' delete processing.
Second, PHP to delete the last character of the method summary:
Method One:
substr ($arr _str,0,strlen ($arr _str)-1);
detailed explanation: substr()
function Syntax:string substr ( string $string , int $start [, int $length ] )
strlen()
function Syntax:int strlen ( string $string )
The principle of this example:
The function is used strlen()
to determine the length of the string $arr_str
, and then the substr()
function is used to intercept $arr_str
and intercept to $arr_str
the penultimate digit. This will remove the last ",".
Usage feeling:
Not recommended, PHP, there are more concise and better use of the way!
Method Two:
Detailed: directly use the substr()
reverse function to cut off the last character;
Use the feeling: still very suitable ~ ~ However, first you have to determine the string must have content, and the last one must not!
Method Three:
detailed explanation: rtrim()
function Syntax:string rtrim ( string $str [, string $character_mask ] )
rtrim-Delete white space characters (or other characters) at the end of a string
Usage feeling:
It's just for this need!
Note: The above method returns the operation result after manipulating the string, and does not change the string itself! Remember to use a variable to receive the result!
Third, summary
The above is a PHP delete string The last character of several methods summary, we have learned it? Hope this article for everyone's study or work can bring certain help.