When performing the duties of creating/manipulating web content, the PHP programmer will need to
modify the lengths of strings, either by padding the strings to be a certain length or removing strings to be a
certain length. PHP provides numerous built-in functions to accomplish these tasks. The following are the
most common functions for modifying string lengths:
• ltrim -
The name of this function identifies its purpose, to trim the string content starting from the
left portion of the string. By default, the ltrim() function will remove all white space characters which
includes the standard space, the horizontal tab(\t), vertical tab (\x0b), new line (\n), carriage return (\r)
and NULL (\0). This function will continue to remove these characters until it reaches a non-white
space character and then the function removal process will terminate.
<?php
$text = \n\t Jane wanted to listen to the troupe play.";
print ltrim($text);
// The new text printed will be "Jane wanted to listen to the
// troupe play."
?>
If there are other characters that must be removed from the left portion of the string, they can be
placed into the second component of the function which is the optional parameter option. When there
is a value in the optional parameter option, all other characters will remain (even white space
characters). If the programmer requires white space characters to be removed also, they must enter
them into the optional parameter option along with the additional required character. With the
optional parameter option, the function will also continue to remove characters until it finds a
character that is not in the optional parameter list and then terminate.
<?php
$text = "\ta) \n\tJane wanted to listen to the troupe play.";
print ltrim($text, "\ta) ");
// The new text printed will be "\n\tJane wanted to listen to
the
// troupe play."
?>
• rtrim -
Is equivalent to the ltrim() function in how it performs, the only difference is it trims from the
end of the string versus the beginning of the string.
<?php
$text = "Jane wanted to listen to the troupe play. \n";
print ltrim($text);
// The new text printed will be "Jane wanted to listen to the
// troupe play." Without the space and line return at the end
?>
• trim -
Is equivalent to the ltrim() and rtrim() functions combined together. The trim() function
trims the characters from the beginning of the string and the end of the string.
<?php
$text = "\n Jane wanted to listen to the troupe play. \n";
print ltrim($text);
// The new text printed will be "Jane wanted to listen to the
// troupe play." Without the space and line return on either end
?>
• str_pad -
This function will insert characters (spaces by default) into a string to force the string to be
a certain length (which is defined in the second parameter of the function).
<?php
$text = "Title";
print str_pad($text, 15);
// The new text printed will be "Title "
?>
A PHP programmer can also change the default character (a space) to another character(s) by placing
the character(s) into the third optional parameter option.
<?php
$text = "Title";
print str_pad($text, 15, "-|");
// The new text printed will be "Title-|-|-|-|-|"
?>
By default the padding is added to the end of the string; however, by entering either
STR_PAD_RIGHT, STR_PAD_LEFT or STR_PAD_BOTH into the fourth optional parameter
option, the programmer can control where the padding will be added. When using the fourth optional
parameter option, the third optional parameter option must also have a character (even if it just a
space).
<?php
$text = "Title";
print str_pad($text, 15, " ", STR_PAD_BOTH);
// The new text printed will be " Title "
?>
modify the lengths of strings, either by padding the strings to be a certain length or removing strings to be a
certain length. PHP provides numerous built-in functions to accomplish these tasks. The following are the
most common functions for modifying string lengths:
• ltrim -
The name of this function identifies its purpose, to trim the string content starting from the
left portion of the string. By default, the ltrim() function will remove all white space characters which
includes the standard space, the horizontal tab(\t), vertical tab (\x0b), new line (\n), carriage return (\r)
and NULL (\0). This function will continue to remove these characters until it reaches a non-white
space character and then the function removal process will terminate.
<?php
$text = \n\t Jane wanted to listen to the troupe play.";
print ltrim($text);
// The new text printed will be "Jane wanted to listen to the
// troupe play."
?>
If there are other characters that must be removed from the left portion of the string, they can be
placed into the second component of the function which is the optional parameter option. When there
is a value in the optional parameter option, all other characters will remain (even white space
characters). If the programmer requires white space characters to be removed also, they must enter
them into the optional parameter option along with the additional required character. With the
optional parameter option, the function will also continue to remove characters until it finds a
character that is not in the optional parameter list and then terminate.
<?php
$text = "\ta) \n\tJane wanted to listen to the troupe play.";
print ltrim($text, "\ta) ");
// The new text printed will be "\n\tJane wanted to listen to
the
// troupe play."
?>
• rtrim -
Is equivalent to the ltrim() function in how it performs, the only difference is it trims from the
end of the string versus the beginning of the string.
<?php
$text = "Jane wanted to listen to the troupe play. \n";
print ltrim($text);
// The new text printed will be "Jane wanted to listen to the
// troupe play." Without the space and line return at the end
?>
• trim -
Is equivalent to the ltrim() and rtrim() functions combined together. The trim() function
trims the characters from the beginning of the string and the end of the string.
<?php
$text = "\n Jane wanted to listen to the troupe play. \n";
print ltrim($text);
// The new text printed will be "Jane wanted to listen to the
// troupe play." Without the space and line return on either end
?>
• str_pad -
This function will insert characters (spaces by default) into a string to force the string to be
a certain length (which is defined in the second parameter of the function).
<?php
$text = "Title";
print str_pad($text, 15);
// The new text printed will be "Title "
?>
A PHP programmer can also change the default character (a space) to another character(s) by placing
the character(s) into the third optional parameter option.
<?php
$text = "Title";
print str_pad($text, 15, "-|");
// The new text printed will be "Title-|-|-|-|-|"
?>
By default the padding is added to the end of the string; however, by entering either
STR_PAD_RIGHT, STR_PAD_LEFT or STR_PAD_BOTH into the fourth optional parameter
option, the programmer can control where the padding will be added. When using the fourth optional
parameter option, the third optional parameter option must also have a character (even if it just a
space).
<?php
$text = "Title";
print str_pad($text, 15, " ", STR_PAD_BOTH);
// The new text printed will be " Title "
?>
Comments
Post a Comment