String manipulation in PHP (i)

Source: Internet
Author: User
Tags rtrim
1. String connector

Half-width symbol "." is a string connector that can concatenate two or more than two strings into a single string. For example:

<?php$name = "topic.alibabacloud.com:"; $url =  echo $name. $url. ". com";? >

The result of the operation is: topic.alibabacloud.com:www.php.cn

2. Remove string leading and trailing spaces and special characters

When entering data, users often inadvertently enter extra spaces, in some cases, spaces and special characters are not allowed in the string, and spaces and special characters in the string are removed.

2.1 Trim () function

The trim () function is used to remove spaces at the beginning and end of a string, and to return a string that is currently stripped of space.

Syntax format: string trim (String str [, String charlist]);

The trim () function parameter str is the string object to manipulate, the parameter charlist is an optional parameter, specifies that a specific character needs to be removed from the pending string, and if the argument is not set, all the optional characters will be deleted. The optional values for the parameter charlist are as follows:

Parameter value/description

NULL, empty value

\ t tab, tab

\ n line break

\x0b Vertical Tab

\ r return character

"" Space

Note: In addition to the above default filter characters, special characters that need to be filtered can also be provided in the charlist

For example:

<?php$str = "\r\r (: @_@ topic.alibabacloud.com @_@:)"; Echo trim ($STR);    Remove the space between the left and right sides of the string echo "<br/>";     Perform line-break echo trim ($str, "\r\r (::)");  Removes special characters from the left and right sides of the string \r\r (::)  ?>

Operation Result:

(: @_@ topic.alibabacloud.com @_@:)

@_@ topic.alibabacloud.com @_@

2.2 LTrim () function

The LTrim () function is used to remove spaces to the left of a string or to specify a string.

The syntax format is as follows:

String LTrim (String str[, String charlist]);

For example:

<?php$str = "(: @_@ topic.alibabacloud.com @_@:) "; Echo LTrim ($STR);    Remove the space to the left of the string echo "<br/>"; Echo LTrim ($str, "(: @_@");    Remove the character to the left of the string @_@:)?>

The result of the operation is:

(: @_@ topic.alibabacloud.com @_@:)

Topic.alibabacloud.com @_@:)

2.3rtrim () function

The RTrim () function is used to remove spaces to the right of a string or to specify a string.

The syntax format is as follows:

String RTrim (String str[, String charlist]);

For example:

<?php$str = "(: @_@ topic.alibabacloud.com @_@:) "; Echo RTrim ($STR);    Remove the space to the right of the string echo "<br/>"; Echo RTrim ($str, "@_@:) ");    Removes the character @_@ to the right of the string:)?>

The result of the operation is:

(: @_@ topic.alibabacloud.com@_@:)

(: @_@ topic.alibabacloud.com

Not to be continued ...

The above is the contents of the string operation in PHP (a), more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.