Difference between revisions of "Working with Strings and Text in PHP"

From Techotopia
Jump to: navigation, search
(PHP printf formatting specifiers)
Line 1: Line 1:
 +
<table border="0" cellspacing="0" width="100%">
 +
<tr>
 +
<td width="20%">[[PHP Arrays|Previous]]<td align="center">[[PHP Essentials|Table of Contents]]<td width="20%" align="right">[[PHP, Filesystems and File I/O|Next]]</td>
 +
<tr>
 +
<td width="20%">PHP Arrays<td align="center"><td width="20%" align="right">PHP, Filesystems and File I/O</td>
 +
</table>
 +
<hr>
 +
 +
 +
<google>BUY_PHP</google>
 +
 +
 
Given that PHP is essentially a vehicle for the deployment of web content, and that much web content is text based, it should come as no surprise that PHP includes a range of features designed to ease the task of manipulating text and strings.
 
Given that PHP is essentially a vehicle for the deployment of web content, and that much web content is text based, it should come as no surprise that PHP includes a range of features designed to ease the task of manipulating text and strings.
  

Revision as of 18:57, 11 May 2009

PreviousTable of ContentsNext
PHP ArraysPHP, Filesystems and File I/O


<google>BUY_PHP</google>


Given that PHP is essentially a vehicle for the deployment of web content, and that much web content is text based, it should come as no surprise that PHP includes a range of features designed to ease the task of manipulating text and strings.

In this chapter we will explore a number of the functions and techniques provided by PHP to enable you, as a web developer, to perform tasks such as changing the case of a string, replacing one part of a piece of text with another peice of text, searching text and much more.


Contents


Changing the Case of a PHP String

PHP provides a number of functions that enable changes to be made to the case of text contained in a string. These functions all take the string to be modified as an argument and return the modifed string. It is important to note that these functions are nondestructive, that is to say they do not make any change to the original string, they simply return a completely new string containing the modification leaving the original string unaltered. The returned string can be assigned to a new variable or used directly in another function (such as a print or echo):

<?php
$myString = "This is a test string.";

$newString = strtoupper($myString);     // Assign converted string to a new variable

echo strtolower($myString);             // Use retruned string in a eacho statement
?>

If a change to the original string is required (as opposed to assigning the modified version to a new variable) the returned string can simply be assigned to the original variable:

<?php
$myString = "This is a test string.";

$myString = strtoupper($myString);     // Assign converted string to the orignal variable

?>

The PHP string functions designed to change the case of a string are listed below with descriptions:

  • strtolower() - Converts entire string to lowercase
  • strtoupper() - Converts entire string to uppercase
  • ucfirst() - Converts the first letter of the sentence to uppercase
  • ucwords() - Converts the first letter of every word to uppercase

Converting to and from ASCII Values

PHP provides the ability to work with ASCII (American Standard Code for Infomration Interchange) values. ASCII maps numerical character codes to standard, human readable characters. There are 127 ASCII characters representing the letters of the alphabext (including upper and lower case), numbers and various punctuation marks. There is also an extended character set which contains 255 characters.

PHP provides two functions for the purpose of converting to and from ASCII codes:

  • ord() - Takes a character as an argument and returns the ASCII code corresponding to that character.
  • chr() - Takes an ASCII code as an argument and retuns the character equivalent.

The following example converts a character to an ASCII code and then reverts it back to the character:

<?php
$character = 'A';

$asciicode = ord($character);

echo "The ASCII code for $character is " . $asciicode . '<br>';

$char = chr($asciicode);

echo "The character for ASCII code for $asciicode is " . $char . '<br>';

?>

When executed the above code will produce the following output:

The ASCII code for A is 65
The character for ASCII code for is A

Printing Formatted Strings in PHP

Those familar with other programming languages such as C and C++ will probably be familiar with the concept of formatting strings and the printf() function. The printf function allows strings to constructed which contain variables and provides a number of formatting options for those variables.

Essentially, printf allows you to specify a string and define how particular variables are to be embedded in that string. fprintf() takes two or more arguments and takes the the following form:

fprintf ( "String", variable1, variable2, ... );

The String represents the string that is to displayed and contains the formatting specifiers. The variables specify the variables that are to be substituted in place of the respective formatting rules.

PHP printf formatting specifiers

The formatting specifiers begin with a '%' character following by a letter to indicate the type of variable to be displayed. For example, a string is represented by %s. Optional rules can be placed between the '%' and the type letter to control formatting.

The following table lists the variable types and the corresponding letter to use when constructing the formatting rule:

<google>ADSDAQBOX_FLOW</google>

SpecifierDescription
%%Displays a percent sign
%bA integer represnted as a binary number
%cA character based on the ASCII value
%dA signed decimal number
%eScientific notation (for example 1.2e+2)
%uAn unsigned decimal number
%fA floating-point number
%FFloating-point number
%oAn octal number
%sA String
%xHexadecimal number in lowercase letters
%XHexadecimal number in uppercase letters

Before we look at the formatting options we first need to see some of the above rules in action. Let's begin by embedding a string variable into a PHP printf function call:

<?php
$myColor = "Green";

printf("My favorite color is %s.", $myColor);

?>

In the above example the %s will be replaced by the value of $myString to create a string which reads:

My favorite color is Green.

We can now consider creating printf statements which combine a number of different formatting rules:

<?php
$myColor = "Green";
$myNumber = 12;

printf("My favorite color is %s and my lucky number is %d.", $myColor, $myNumber);

?>

In the above example the %s will be replaced by the value of $myString and the %d will be replaced by the decimal value of $myNumber to create a string which reads:

My favorite color is Green and my lucky number is 12.

Suppose we wanted to express our lucky number as a hexadecimal value (as all good software engineers do). All we would need to do is replace the %d with a %x:

$formatted = printf ("My favorite color is %s and my lucky number is %x.", $myString);

thereby generating the following output:

My favorite color is Green and my lucky number is c.

Finally we need to look at the formatting options which may be specified after the '%' character.

  • + - Forces both + or - in front of numbers. By default, only negative numbers display their polarity.
  • 'n - Specifies what to use as padding (represented by n). Used in conjunction with the width specifier and defaults to space. Eg: %'_20s specified that '_' should be used as padding.
  • - Left-justifies the value
  • [0-9] Specifies the minimum width to be used by the variable - used in conjuction with the padding character
  • .[0-9] Specifies the number of decimal digits or maximum string length

As always, some examples will hopefully help to clarify these options:

To limit a floating point number to 2 decimal places:

<?php

$myColor = "Green";
$myNumber = 12.2089987;
printf("My number is %.2f.", $myNumber);

?>

To pad the formatting with '_' characters to a width of 12 characters:

<?php

$myNumber = 12.2089987;
printf("My number is %'_12f.", $myNumber);

?>

resulting in the following output:

My number is ___12.208999.

Finding the Length of a PHP String

The length of a PHP string can be obtained using the strlen() function. The strlen() function takes the string to be measured as an argument and returns the length value:

<?php

$myString = "This is a short string";

$strLength = strlen ($myString);

echo "The string length is $strLength.<br>";

?>

Converting a String into a PHP Array

Any string in PHP can be converted into a PHP array (see the PHP Arrays chapter for an overview of arrays). This is achieved using the PHP explode() function. The explode() function takes three arguments and returns an array:

  • delimiter - the character that is to be used as the break point between array elements. For example a space character or a comma.
  • string - the string that is to be converted into an array.
  • divisions - (Optional). Sepcified the maximim number of array elements the string should be broken up into. When the limit is reach the final array element contains the remainder of the string.

The following example shows the explode() function in use:

<?php
$myString = "This is a short string";

$myArray = explode($myString);

print_r($myArray);
?>

The above example will result in the following output, which shows each word in the sentance assigned to an array element:

Array ( [0] => This [1] => is [2] => a [3] => short [4] => string )

Removing Leading and Trailing Whitespace from a PHP String

The leading and trailing whitespcae can be removed from a string using the PHP trim() function. Whitespace characters are defined by PHP to be space, tab, newline, carriage return, NULL and vertical tab characters.

The trim() command takes the string to be trimmed as an argument and returns a modified version of the string. The function is non-destructive, in that it does not modify the original string.

The following example trims the whitespace from the specified string:

<?php
$string = "          This is a string with lots of whitespace           ";

echo "Before trim [$string]";

$trimmedString = trim($string);

echo "After trim [$trimmedString]";

?>

Comparing Strings in PHP

In developing web applications it is extremely common to need to compare two strings. PHP provides a number of functions that make it easy to perfrom a number of different string comparison tasks:

  • strcmp() - Accepts two strings as arguments, performs a case-sensitive comparison and returns a value depending on the match.
  • strcasecmp - Accepts two strings as arguments, performs a case-insensitive comparison and returns a value depending on the match.
  • strncmp() - Accepts three arguments - the two strings to be compared and the number of characters to be included in the comparison. Performs a case-sensitive comparison of specified number of characters from each string and returns a value depending on the result of the match.
  • strncasecmp() - Accepts three arguments - the two strings to be compared and the number of characters to be included in the comparison. Performs a case-insensitive comparison of specified number of characters from each string and returns a value depending on the result of the match.

String Comparison Functions Return Value

The string comparison function perform an ASCII based comparison of each character. If the ASCII codes of the two strings match then the functions return 0. If the first string has a ASCII value less than the second a negative number is returned. If it is greater a positive number is returned.

Accessing and Modifiying Characters in String

The individual characters in a string can be accessed and modified by their position in a string. To achieve this, simply place the position of the required character in braces {} after the string variable name. Keep in mind that indexes are zero based, so the first character in a string in index position 0, not position 1.

For example to access the 2nd character in a string:

<?php

$myString = "abcdefghijklmn";

$myChar = $myString{1};

echo "2nd Char = $myChar";

?>

The above example will result in the following output:

2nd Char = b

Similarly the character position can be assigned a new value using the assigment operator:

<?php
$myString = "abcdefghijklmn";

echo "Before change = $myString";

$myString{1} = 'B';

echo "Before change = $myString";
?>

The result from the above output will read as follows:

Before change = abcdefghijklmn
Before change = aBcdefghijklmn

Searching for Characters and Substrings in a PHP String

PHP provides the ability to find the position of a specified sequence of characters (better known as a substring) within a string via the strpos() and strrpos() functions.

The strpos() function finds the first occurance a substring and takes two mandatory and one optional argument. The first argument is the string in which the search should be performed, and the second the substring for which to search. The optional third argument tells PHP the point in the string to initiate the search. The function returns boolean false (0) if no match is found, otherwise it returns the index into the string of the occurence.

Note that if a string begins with the substring there will be some confusion since the function will return 0, which could equally be interpreted as a failure to find a match. The key point to understand here is that a failure to find a match will return a boolean zero. A match starting at position zero will return a numeric 0. To resolve this issue it is best to ensure that you are comparing like variable types. You may recall from the PHP Operators chapter about comparing variables to ensure they are of the same type and value using the === and !== operators. We can use this technique to vefify we are getting boolean false, and not an integer 0:

<?php
if (strpos("Hello World", "Hello") !== false)
     echo 'Match found';
?>

Similarly, the strrpos() function returns the position of the last occurance of the substring.

Extracting and Replacing Substrings in PHP

A sub-section of any PHP string can be extracted and replaced using the substr() and substr_replace() functions.

The substr() function takes two arguments which represent the source string and the start index. An optional third argument specifies the length of the substring (if no length is specified the end of the string is assumed). The function returns the requested substring:


<?php

$myString = "There is a cat in the tree.";

$subString = substr ($myString, 11, 3);

echo "subString = $subString <br>";

?>

The above example will extract the word cat from the string.

The substr_replace() function takes up to four arguments. The first is the source string on which the replace is to be performed. The second argument is the replacement string. The third specifies the index offset of the start of the replacement. The optional fourth argument defines number of characters from the offset point to include in the replacement (useful if your replacement text is shorter than the original text being replaced).

We can now extend our example to replace some text in our string:

<?php

$myString = "There is a cat in the tree.";

echo "Original String = $myString<br>";

$myString = substr_replace ($myString, "dog", 11, 3);

echo "New String = $myString<br>";

?>

The above example will result in the following output:

Original String = There is a cat in the tree.
New String = There is a dog in the tree.

It is perfectly valid to replace one substring with another of different length. It is also possible to simply remove a substring by simply passing in an empty replacement string to the substr_replace() fucntion.

Replacing All Instances of a Word in a PHP String

All the instances of a word in a string can be replaced using the PHP str_replace() function. str_replace() takes 3 required and one optional argument. The first argument represents the string to be replaced, the second the replacement value and the third the target string. The optional fourth argument is a reference to a variable where the results of the replacement will be stored for subsequent inspection.

A simple example is as follows:

<?php
$myString = "There is a cat in the tree, and I think it is my cat!";

echo "Original String = $myString<br>";

$myString = str_replace ("cat", "dog", $myString);

echo "New String = $myString<br>";
?>

Resulting in the following output:

Original String = There is a cat in the tree, and I think it is my cat!
New String = There is a dog in the tree, and I think it is my dog!

If the search and replacement values are arrays of words then each word in the search array is replaced by the corresponding value in the replacement array. For example:

<?php
$myString = "There is a cat in the tree, and I think it is my cat!";

echo "Original String = $myString<br>";

$myString = str_replace (array("is", "cat", "tree"), array("was", "dog", "car"), $myString);

echo "New String = $myString<br>";
?>

The above example results in the following output:

Original String = There is a cat in the tree, and I think it is my cat!
New String = There was a dog in the car, and I think it was my dog!

A case insensitive search and replace can similarly be performed using the eregi_replace() function which takes the same arguments as str_replace()