PHP String - How To Remove White Spaces from the Beginning and/or the End of a String
Interview Question Database For Software Developers
|
|
| How To Remove White Spaces from the Beginning and/or the End of a String | | How To Remove White Spaces from the Beginning and/or the End of a String? - PHP Script Tips - PHP Built-in Functions for Strings | | By: FYIcenter.com | There are 4 PHP functions you can use remove white space characters from the beginning and/or the end of a string:
- trim() - Remove white space characters from the beginning and the end of a string.
- ltrim() - Remove white space characters from the beginning of a string.
- rtrim() - Remove white space characters from the end of a string.
- chop() - Same as rtrim().
White space characters are defined as:
- " " (ASCII 32 (0x20)), an ordinary space.
- "\t" (ASCII 9 (0x09)), a tab.
- "\n" (ASCII 10 (0x0A)), a new line (line feed).
- "\r" (ASCII 13 (0x0D)), a carriage return.
- "\0" (ASCII 0 (0x00)), the NULL-byte.
- "\x0B" (ASCII 11 (0x0B)), a vertical tab.
Here is a PHP script example of trimming strings:
<?php
$text = "\t \t Hello world!\t \t ";
$leftTrimmed = ltrim($text);
$rightTrimmed = rtrim($text);
$bothTrimmed = trim($text);
print("leftTrimmed = ($leftTrimmed)\n");
print("rightTrimmed = ($rightTrimmed)\n");
print("bothTrimmed = ($bothTrimmed)\n");
?>
This script will print:
leftTrimmed = (Hello world! )
rightTrimmed = ( Hello world!)
bothTrimmed = (Hello world!)
| | ID: 388 | Rank: 1086 | Votes: 0 | Views: 38 | Submitted: 20070422 |
Copyright © 2009 FYIcenter.com
All rights in the contents of this Website are reserved by the individual author.
No part of the contents may be reproduced in any form without author's permission.
|
|
|