Implementation of the TRIM function code in PHP _php tips

Source: Internet
Author: User
Remove the space before and after.

Suppose there is a string "DDD dd D" that becomes "DDD DD D" after trim ().

You can remove extra spaces (including tabs) on either side, but you can't remove the middle space.
The trim () function in PHP is the same as the trim () function in ASP, and if you touch an ASP, it will be used naturally.
The usage is simple, add the following to the variable that you want to remove the space:
<?php
Echo Trim ($ variable);
?>

Generally used in the user's password processing.

Definitions and usage
The trim () function deletes white space characters and other predefined characters from both ends of the string.

Grammar
Trim (string,charlist) parameter description
String required. Specify the string to check.
Charlist Optional. Specify the string to convert. If this argument is omitted, all of the following characters are deleted:
"" "-NULL
"\ T"-tab
"\ n"-New line
"\X0B"-Portrait list character
"\ r"-Carriage return
""-Plain white space character
Example 1
Copy Code code as follows:

<body>
<?php
$str = "Hello world!";
echo "without Trim:". $STR;
echo "<br/>";
echo "with Trim:". Trim ($STR);
?>
<body>

Output:
Copy Code code as follows:

Without Trim:hello world!
With Trim:hello world! HTML Source code:


Example 2
Copy Code code as follows:

<?php
$str = "\r\nhello world!\r\n";
echo "without Trim:". $STR;
echo "<br/>";
echo "with Trim:". Trim ($STR);
?>

Output:

Without Trim:hello world!
With Trim:hello world! Html

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.