PHP removes the last character of the string substr () Usage _php tutorial

Source: Internet
Author: User
Tags rtrim
Used in today's project to remove the last character in a string
The original string 1,2,3,4,5,6,
Remove the last character "," and the end result is 1,2,3,4,5,6
The code is as follows:

The code is as follows:


$str = "1,2,3,4,5,6,";
$newstr = substr ($str, 0,strlen ($STR)-1);
Echo $newstr;


The system comes with functions that can also achieve this effect, two methods:
1) substr ($str, 0,-1)
2) RTrim ($str, ",")

Substr
Takes a partial string.
Syntax: String substr (string string, int start, int [length]);
return value: String
Function type: Data processing
Content Description
This function takes the string starting at the start of the string to take a length character. If start is negative, it is counted from the end of the string. If the omitted parameter length exists, but is a negative number, it is taken to the penultimate length character.
Usage examples

The code is as follows:


echo substr ("abcdef", 1, 3); Returns "BCD"
echo substr ("ABCdef",-2); Back to "EF"
echo substr ("ABCdef",-3, 1); Return "D"
echo substr ("abcdef", 1,-1); Back to "BCDE"
?>


PHP RTrim () function
Definition and usage
The RTrim () function removes whitespace characters or other predefined characters starting at the end of the string. The same chop () function.
Grammar
RTrim (string,charlist) parameter description
string is required. Specifies the string to convert.
Charlist is optional. Specifies which characters are removed from the string.
If this parameter is not set, all of the following characters are removed:
"+"-ASCII 0, NULL
"\ T"-ASCII 9, tab
"\ n"-ASCII 10, New line
"\X0B"-ASCII 11, Vertical tab
"\ r"-ASCII 13, enter
""-ASCII 32, space

Usage examples

http://www.bkjia.com/PHPjc/756103.html www.bkjia.com true http://www.bkjia.com/PHPjc/756103.html techarticle used today in the project, remove the last word in the string Fu Yuan string 1,2,3,4,5,6, remove the last character, the final result is 1,2,3,4,5,6 code as follows: Code: $STR = ...

  • Related Article

    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.