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...

Data Encryption

Data encryption is the process of scrambling stored or transmitted information so that it is unintelligible until it is unscrambled by the intended recipient. The intended recipient can then decode (or decrypt) the information. PHP offers multiple means to make this happen. However, none of these solutions are very effective without the applications running on secure servers and connections. The following are a list of the more common encryption functions in PHP: • md5() - MD5 is a third-party hash algorithm that PHP can use to create a digital fingerprint of a piece of data. It is next to impossible to (efficiently) recover the original text when a piece of data has been encrypted with the md5 hash algorithm. It is also vastly unlikely that any different text string will create an identical hash - a 'hash collision'. These properties make hashes ideally suited for storing an application's passwords because although an attacker may compromise a part of the system...

Delimiting PHP Code

                           PHP was originally designed to be used in conjunction with a web server, and in the case of the LAMP architecture, the Apache Web Server.  PHP applications are designed embedding PHP scripts within a web page along with its HTML.  Unlike standard HTML pages which are sent directly from the web server to the end user, PHP files are first interpreted by the PHP application which then converts the PHP script into another form for display.  This process eliminates the end user from being able to see the original PHP script that was embedded in the HTML and provides  true interaction in HTML files.  This process is similar to proprietary applications such as ASP and Coldfusion; however, PHP is Open Source and cross- platform. PHP Tags             PHP scripts are distinguished from the HTML scripts by using delimiting characters ...