Php deletes the left-side and right-side space functions. Deleting functions in php is much more specific than js. in addition to the trim () function, there are also ltrim () and rtrim () functions. they need to delete spaces between front and back, respectively, in addition to the three functions, you can also use the positive deletion function in php, which is much more specific than js. in addition to the trim () function, there are also ltrim () and rtrim () functions, they need to delete spaces between the front and back of each other. in addition to the three functions, they can also use regular expression deletion.
Ltrim () function
Ltrim ($ str, $ charlist)
// $ Str indicates the string to be processed;
// $ Charlist is a special character to be deleted. if it is null, spaces on the left are removed.
The code is as follows: |
|
$ T = "... I'm Jacky ..."; Echo "a". $ t ." "; $ Left = ltrim ($ t ); Echo "a". $ left ." "; $ Lleft = ltrim ($ left ,"."); Echo $ lleft; ?> |
Rtrim () function
Rtrim ($ str, $ charlist)
// $ Str indicates the string to be processed;
// $ Charlist is a special character to be deleted. if it is null, spaces on the right are removed.
The code is as follows: |
|
$ A = "htm "; Echo $ a. "l "." "; Echo rtrim ($ a). "l "; ?> |
Trim () function
First remove leading and trailing spaces
The code is as follows: |
|
$ Str = "This line containstliberal rn use of whitespace. nn "; // First remove the leading and trailing spaces $ Str = trim ($ str ); // Remove the two or more spaces $ Str = preg_replace ('/s (? = S)/', '', $ str );
// Replace a non-space with a space. $ Str = preg_replace ('/[nrt]/', '', $ str ); |
The preceding example can remove all unnecessary spaces. Use TRim () to remove leading and trailing spaces, and then use preg_replace () to remove repeated spaces.
Use regular expressions to replace and improve functions
Php removes spaces at the beginning and end of the string (including the full angle)
The code is as follows: |
|
$ Str = "www.bkjia.com "; $ Str = mb_ereg_replace ('^ (|) +', '', $ str ); $ Str = mb_ereg_replace ('(|) + $', '', $ str ); Echo mb_ereg_replace (''," n ", $ str ); ?> |
The trim () function, as well as the ltrim () and rtrim () functions, respectively. they need to delete the spaces between the front and back. in addition to the three functions, they can also use the positive...