Skip to main content

Modify String Length

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 "
?>

Comments

Popular posts from this blog

PHP INTRODUCTION

                     PHP  (recursive acronym for  PHP: Hypertext Preprocessor ) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. PHP stands for  P HP:  H ypertext  P reprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) PHP is an open source software PHP is free to download and use Why PHP? PHP runs on different platforms (Windows, Linux, Unix, etc.) PHP is compatible with almost all servers used today (Apache, IIS, etc.) PHP is FREE to download from the official PHP resource:  www.php.net PHP is easy to learn and runs efficiently on the server side What can PHP do? Anything. PHP is mainly focused on server-side scripting, so you can...

Self Joins

A table name is always ambiguous when table is joined to itself using a self-join. For example, the Country table in the world database contains an IndepYear column indicating the year in which each country achieved independence. To find all countries that have the same year of independence as some given country, a self-join can be used. However, the query cannot be written like this: mysql> SELECT IndepYear, Name, Name -> FROM Country JOIN Country -> ON IndepYear = IndepYear AND Name = 'Qatar'; ERROR 1066 (42000): Not unique table/alias: 'Country' Furthermore, the ambiguity cannot be removed from column references by preceding them with table name qualifiers because the names remain identical: mysql> SELECT Country.IndepYear, Country.Name, Country.Name -> FROM Country JOIN Country -> ON Country.IndepYear = Country.IndepYear -> AND Country.Name = 'Qatar'; ERROR 1066 (42000): Not unique table/alias: 'Country' It ...