ltrim(), rtrim() and trim() Trimming the string

Remove whitespace from string ends

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
Remove blank space from left side of any string by using ltrim() function.
$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

Remove blank space present inside a string

For a user ID that explicitly disallows spaces, validate the entered value and show an error rather than silently changing a user’s chosen identifier. We can search and inform the user about this or remove the blank space from the string variable.
We will use str_replace() to remove blank space from a string.
$string = "AB CD  XYZ";
$new_string = str_replace(" ", "", $string);
echo $new_string; // ABCDXYZ

Searching for presence of white space.

For a literal space, strstr() can test whether one occurs. For any whitespace character, including tabs and newlines, use a pattern such as preg_match("/\s/", $string).
$string='ABCD  XYZ';
if (strstr($string,' ')) {
echo " Blank space is  present  ";
}else{
echo " Blank space is NOT  present ";
}

String Functions first letter of every word to capitalize by using ucwords Change all alphabetic chars to lowercase Checking all lower case or upper case chars in a string


Subscribe to our YouTube Channel here



plus2net.com







panta

29-12-2014

nice tutorials , much love and appreciations




PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer