Summary of three methods for the last character of the PHP delete string _php tips

Source: Internet
Author: User
Tags rtrim

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.