PHP trim() function
Mga halimbawa
Alisin ang mga character sa bawat dakong baba ng string ("Hello" na may "He" at "World" na may "d!" sa loob):
<?php $str = "Hello World!"; echo $str . "<br>"; echo trim($str,"Hed!"); ?>
Paglilinang at paggamit
Ang function na trim() ay nag-alisin ng mga puti na nasa bawat dakong baba at itaas ng string o iba pang naipinalagay na character.
Mga kaugnay na function:
pangungusap
trim(s tring,charlist)
parameter | pagsasalita |
---|---|
string | Mga kinakailangan. Ang paglilitis ng string na dapat suriin. |
charlist |
Optional. Specify which characters to remove from the string. If omitted, the following characters are removed:
|
Technical Details
Return Value: | Return the modified string |
PHP Version: | 4+ |
Update Log: | Added in PHP 4.1: charlist Parameter. |
More Examples
Example 1
Remove spaces from both sides of the string:
<?php $str = " Hello World! "; echo "Not using trim: " . $str; echo "<br>"; echo "Use trim: " . trim($str); ?>
The HTML output of the above code is as follows (please check the source code):
<!DOCTYPE html> <html> <body> Not using trim: Hello World! <br>Using trim: Hello World! </body> </html>
The browser output of the above code is as follows:
Not using trim: Hello World! Use trim: Hello World!
Example 2
Remove newline characters (\n) from both sides of the string:
<?php $str = "\n\n\nHello World!\n\n\n"; echo "Not using trim: " . $str; echo "<br>"; echo "Use trim: " . trim($str); ?>
The HTML output of the above code is as follows (please check the source code):
<!DOCTYPE html> <html> <body> Not using trim: Hello World! <br>Use trim: Hello World! </body> </html>
The browser output of the above code is as follows:
Not using trim: Hello World! Use trim: Hello World!