trim() removes whitespace or selected characters from both ends of a string. Use ltrim() or rtrim() when only one side should change.
$value = ' PHP tutorial ';
echo trim($value); // PHP tutorial
$text= " This is my text";
echo $text;
$text=ltrim($text);
echo $text;
Same way we can remove blank space from right side of any string by using rtrim() function.
$text= "This is my text ";
echo $text;
$text=rtrim($text);
echo $text;
To remove extra blank space, line break, carriage return from both sides ( left and right ) of a string we can use trim() function
$text= " This string has blank space at both sides ";
echo $text;
$text=trim($text);
echo $text;
Use the optional character mask to remove selected trailing characters:
$str=' Hello +';
echo rtrim($str,'+');
The plus sign is removed from the end. To remove several specified trailing characters, pass a character mask.
$str=' Hello Welcome';
echo rtrim($str,'come');
Output (the rtrim() character mask strips any trailing characters found in come, not the exact word):Hello Wel
$string = "AB CD XYZ";
$new_string = str_replace(" ", "", $string);
echo $new_string; // ABCDXYZ
preg_match("/\s/", $string).
$string='ABCD XYZ';
if (strstr($string,' ')) {
echo " Blank space is present ";
}else{
echo " Blank space is NOT present ";
}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| panta | 29-12-2014 |
| nice tutorials , much love and appreciations | |