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 do anything any other CGI program can do, such as collect form data, generate dynam

MySQL General Architecture

        MySQL operates in a networked environment using a client/server architecture. In other words, a central  program acts as a server, and various client programs connect to the server to make requests. A MySQL  installation has the following major components: MySQL Server, Client programs and MySQL non client  utilities.  MySQL Server MySQL Server, or mysqld, is the database server program. The server manages access to the actual  database (schema) on disk and in memory. MySQL Server is multi-threaded and supports many  simultaneous client connections. Clients can connect via several connection protocols. For managing  database contents, the MySQL server features a modular architecture that supports multiple storage engines  that handle different types of tables (for example, it supports both transactional and non-transactional  tables). Keep in mind the difference between a server and a host. The server is software (the MySQL server  program mysqld). Server characteristi