Manipulating Strings with PHP

PHP provides many functions that will transform a string argument, subtly or radically, as you'll soon see.

Cleaning Up a String with trim() and strip_tags()

When you acquire text from the user or a file, you can't always be sure that you haven't also picked up whitespace at the beginning and end of your data. The trim() function shaves any whitespace characters, including newlines, tabs, and spaces, from both the start and end of a string. It accepts the string to be modified, returning the cleaned-up version:

 $text = "\t\t\tlots of room to breathe          "; $text = trim( $text ); print $text; // prints "lots of room to breathe"; 

Of course, removing all the whitespace might be more work than you require. You might want to keep whitespace at the beginning of a string but remove it from the end. You can use PHP's rtrim() function exactly the same as you would use trim(). Only whitespace at the end of the string argument is removed, however:

 $text = "\t\t\tlots of room to breathe          "; $text = rtrim( $text ); print $test; // prints "            lots of room to breathe"; 

PHP provides the ltrim() function to strip whitespace only from the beginning of a string. Once again, this is called with the string you want to transform and returns a new string, shorn of tabs, newlines, and spaces:

 $text = "\t\t\tlots of room to breathe     "; $text = ltrim( $text ); print $test; // prints "lots of room to breathe     "; 

PHP, by its nature, tends to work with markup text. It is not unusual to have to remove tags from a block to be able to present it without formatting. PHP provides the strip_tags() function for this purpose. The strip_tags() function accepts two arguments. The first is the text to transform. The second argument is optional and should be a list of HTML tags that strip_tags() can leave in place. The tags in the exception list should not be separated by any characters.

 $string = "I <i>simply</i> will not have it,<br>said Mr Dean<p><b>The end</b>"; print strip_tags($string, "<br><p>"); 

In the preceding code snippet, we create an HTML-formatted string. When we call strip_tags(), we pass it the $string variable and a list of exceptions. The result is that the <p> and <br> tags are left in place and all other tags are stripped out.

Replacing a Portion of a String Using substr_replace()

The substr_replace() function works similarly to substr() except that it allows you replace the portion of a string that you extract. The function requires three arguments: the string you are transforming, the text you want to add to it, and the starting index. It also accepts an optional length argument. The substr_replace() function finds the portion of a string specified by the starting index and length arguments, replacing this portion with the string provided in the replace string argument and returning the entire transformed string.

In the following code snippet used to renew a user's membership code, we must change its second two characters:

 <? $membership = "mz01xyz"; $membership = substr_replace($membership, "02", 2, 2); print "New membership number: $membership<p>"; // prints "New membership number: mz02xyz" ?> 

The result of this code is that the old membership number, "mz01xyz", becomes the new membership number "mz02xyz".

Replacing Substrings Using str_replace

The str_replace() function replaces all instances of a string within another string. It requires three arguments: a search string, the replacement string, and the string on which this transformation is to be effected. The function returns the transformed string. The following example uses str_replace() to change all references from 2001 to 2002 within a string:

 $string = "Site contents copyright 2001. "; $string .= "The 2001 Guide to All Things Good in Europe"; print str_replace("2001","2002",$string); 

As of PHP 4.0.5, str_replace() has been enhanced to accept arrays as well as strings for all its arguments. This allows us to perform multiple search-and-replace operations on a subject string and even on more than one subject string. Consider the following snippet, for instance:

 <?php $source = array(         "The package which is at version 4.2 was released in 2001",         "The year 2001 was an excellent period for PointyThing4.2" ); $search  = array("4.2", "2001"); $replace = array("5.0", "2002"); $source = str_replace($search, $replace, $source); foreach($source as $str) {        print "$str<br>"; } // prints: // The package which is at version 5.0 was released in 2002 // The year 2002 was an excellent period for PointyThing5.0 ?> 

When str_replace() is passed an array of strings for its first and second arguments, it attempts to switch each search string with its corresponding replace string in the text to be transformed. When the third argument is an array, str_replace() returns an array of strings. The search-and-replace operation is executed on each string in the array.

Converting Case

PHP provides several functions that allow you to convert the case of a string. Changing case is often useful for string comparisons. To get an uppercase version of a string, use the function strtoupper(). This function requires only the string that you want to convert and returns the converted string:

 $membership = "mz02xyz"; $membership = strtoupper($membership); print "$membership<br>"; // prints "MZ02XYZ" 

To convert a string to lowercase characters, use the function strtolower(). Once again, this function requires the string you want to convert and returns a converted version:

 $membership = "MZ02XYZ"; $membership = strtolower($membership); print "$membership<br>"; // prints "mz02xyz" 

PHP also provides a case function that has a useful cosmetic purpose. The ucwords() function makes the first letter of every word in a string uppercase. In the following snippet, we make the first letter of every word in a user-submitted string uppercase:

 $full_name = "violet elizabeth bott"; $full_name = ucwords($full_name); print $full_name; // prints "Violet Elizabeth Bott" 

Although this function makes the first letter of each word uppercase, it does not touch any other letters. So if the user had had problems with her Shift key in the previous example and submitted VIolEt eLIZaBeTH bOTt, our approach would not have done much to fix the string. We would have ended up with VIolEt ELIZaBeTH BOTt, which isn't much of an improvement. We can deal with this problem by making the submitted string lowercase with strtolower() before invoking ucwords():

 $full_name = "VIolEt eLIZaBeTH bOTt"; $full_name = ucwords(strtolower($full_name)); print $full_name; // prints "Violet Elizabeth Bott" 

Wrapping Text with wordwrap() and nl2br()

When you present plain text within a Web page, you are often faced with the problem that newlines are not displayed, and your text runs together into a featureless blob. The nl2br() function is a convenience method that converts every newline into an HTML break. So

 $string  = "one line\n"; $string .= "another line\n"; $string .= "a third for luck\n"; print nl2br($string); 

will print

 one line<br /> another line<br /> a third for luck<br /> 

Notice that the <br> tags are output in XHTML-compliant form. This feature was introduced in PHP 4.0.5.

The nl2br() function is great for honoring newlines that are already in the text you are converting. Occasionally, though, you may want to add arbitrary line breaks to format a column of text. The wordwrap() function is perfect for this task. wordwrap() requires one argument: the string to be transformed. By default, wordwrap() wraps lines every 75 characters and uses '\n' as its line break character. So the code snippet

 $string  = "Given a long line, wordwrap() is useful as a means of "; $string .= "breaking it into a column and thereby making it easier to read"; print wordwrap($string); 

would output

 Given a long line, wordwrap() is useful as a means of breaking it into a column and thereby making it easier to read 

Because the lines are broken with the character '\n', the formatting does not show up in HTML mode, of course. wordwrap() has two more optional arguments: a number representing the maximum number of characters per line and a string representing the end of line string you would like to use. So applying the function call

 print wordwrap( $string, 24, "<br>\n" ); 

to our $string variable, our output would be

 Given a long line,<br> wordwrap() is useful as<br> a means of breaking it<br> into a column and<br> thereby making it easier<br> to read 

The wordwrap() function doesn't automatically break at your line limit if a word has more characters than the limit. You can, however, use an optional fourth argument to enforce such a break. The argument should be a positive integer. So using wordwrap() in conjunction with the fourth argument, we can now wrap a string, even where it contains words that extend beyond the limit we are setting. This snippet

 $string = "As usual you will find me at http://www.witteringonaboutit.com/"; $string .= "chat/eating_green_cheese/forum.php. Hope to see you there!"; print wordwrap( $string, 24, "<br>\n", 1 ); 

will output

 As usual you will find<br> me at<br> http://www.witteringonab<br> outit.com/chat/eating_gr<br> een_cheese/forum.php.<br> Hope to see you there! 

instead of

 As usual you will find<br> me at<br> http://www.witteringonaboutit.com/chat/eating_green_cheese/forum.php. <br> Hope to see you there! 

Breaking Strings into Arrays with explode()

The delightfully named explode() function is similar in some ways to strtok(). But explode() will break up a string into an array, which you can then store, sort, or examine as you want. The explode() function requires two arguments: the delimiter string that you want to use to break up the source string and the source string itself. The function optionally accepts a third argument, which will determine the maximum number of pieces the string can be broken into. The delimiter string can include more than one character, all of which will form a single delimiter (unlike multiple delimiter characters passed to strtok(), each of which will be a delimiter in its own right). The following snippet breaks up a date and stores the result in an array:

 $start_date = "2002-01-12"; $date_array = explode("-", $start_date); // $date[0] == "2002" // $date[1] == "01" // $date[2] == "12" 

Now that your head is filled with PHP string functions, let's move on to MySQL string functions, many of which perform the same tasks.



Sams Teach Yourself PHP, MySQL and Apache in 24 Hours
Sams Teach Yourself PHP, MySQL and Apache in 24 Hours
ISBN: 067232489X
EAN: 2147483647
Year: 2005
Pages: 263

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net