PHP removes the usage of the last character substr () of the string

Source: Internet
Author: User
Used in today's project to remove the last character in the string
Original string 1, 2, 3, 4, 5, 6,
Remove the last character "," and the final result is 1, 2, 3, 4, 5, 6.
The Code is as follows:
$ STR = "1, 2, 3, 4, 5, 6 ,";
$ Newstr = substr ($ STR, 0, strlen ($ Str)-1 );
Echo $ newstr;

The built-in functions of the system can also achieve the following two methods:
1) substr ($ STR, 0,-1)
2) rtrim ($ STR ,",")


Substr

Take some strings.

Syntax:String substr (string, int start, int [length]);

Return Value: String

Function Type: Data Processing

Description

This function extracts the start character of the string from the start character. If start is a negative number, it is counted from the end of the string. If the parameter length can be omitted, but it is a negative number, it indicates that the maximum length is obtained.

Example

<?
Echo substr ("abcdef", 1, 3); // return "BCD"
Echo substr ("abcdef",-2); // return "Ef"
Echo substr ("abcdef",-3, 1); // return "D"
Echo substr ("abcdef", 1,-1); // return "bcde"
?>


PHP rtrim () function definition and usage

The rtrim () function removes white spaces or other predefined characters from the end of a string. The same as the chop () function.

Syntax
rtrim(string,charlist)
Parameters Description
String Required. Specifies the string to be converted.
Charlist

Optional. Specifies which characters are deleted from the string.

If this parameter is not set, all of the following characters are deleted:

  • "\ 0"-ASCII 0, null
  • "\ T"-ASCII 9, Tab
  • "\ N"-ASCII 10, New Line
  • "\ X0b"-ASCII 11, vertical Tab
  • "\ R"-ASCII 13, press ENTER
  • ""-ASCII 32, space

Example

 

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.