Three ways to remove the last character of a string in a PHP program

Source: Internet
Author: User
Tags constant definition rtrim

Common syntax formats:

foreach ($arr as $key = = $value) {
$arr _str = $arr [' x_id ']. ‘,‘ . $arr _str;
}

Assume that the characters in the character array $arr are

Arr[0] = ' a ';
Arr[1] = ' B ';
ARR[2] = ' C ';

Then, after the concatenation of the $arr _str string is a,b,c, this time, we need to the last character ', ' to delete processing.

Two ways to remove the last character in PHP are summarized:

Method One:

substr ($arr _str,0,strlen ($arr _str)-1);
Explanation: substr () function syntax: string substr (string $string, int $start [, int $length])

strlen () function syntax: int strlen (string $string)

This example principle:
First use the strlen () function to determine the length of the string $arr_str, and then use the substr () function to intercept the $ARR_STR and intercept to the second-lowest of the $arr_str. This will take the final "," removed.

Usage Experience:

Not recommended, PHP has a more concise and better way to use!

Method Two:

substr ($arr _str, 0,-1)
Detailed: Use the substr () function directly in reverse to cut off the last character;
Use the feeling: still very suitable ~ ~ However, first you have to make sure that the string must have content, and the last one must not!
Method Three:

RTrim ($arr _str, ",")
Explanation: RTrim () function syntax: string RTrim (String $str [, String $character _mask])

Rtrim― Delete whitespace characters (or other characters) at the end of a string

System constant Definition
Go to the thinphp manual to find
echo "<br>". " The root directory address of the Web site ". __root__." ";
echo "<br>". " Portal file Address ". __app__." ";
echo "<br>". " Current module address ". __url__." ";
echo "<br>". " Current URL address ". __self__." ";
echo "<br>". " Current action address ". __action__." ";
echo "<br>". " Template directory for the current module ". __current__." ";
echo "<br>". " The current operation name. Action_name. " ";
echo "<br>". " Current project directory. App_path. " ";
echo "<br>". " The current project name. App_name. " ";
echo "<br>". " The template directory for the current project. App_tmpl_path. " ";
echo "<br>". " The public file directory for the project. App_public_path. " ";
echo "<br>". " The configuration file directory for the project. Config_path. " ";
echo "<br>". " The public file directory for the project. Common_path. " ";
Automatically caches all information related to the table
echo "<br>". " The data file directory for the project. Data_path. "Data directory under Runtime";
echo "<br>". " ". Group_name. "";
echo "<br>". " ". is_cgi.";
echo "<br>". " ". Is_win.";
echo "<br>". " ". Lang_set. "";
echo "<br>". " ". Log_path. "";
echo "<br>". " ". Lang_path. "";
echo "<br>". " ". Tmpl_path. "";
JS put in the location for multiple applications of public resources
echo "<br>". " ". Web_public_path. "";

Three ways to remove the last character of a string in a PHP program

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.