Book HomePHP CookbookSearch this book

Chapter 1. Strings

Contents:

Introduction
Accessing Substrings
Replacing Substrings
Processing a String One Character at a Time
Reversing a String by Word or Character
Expanding and Compressing Tabs
Controlling Case
Interpolating Functions and Expressions Within Strings
Trimming Blanks from a String
Parsing Comma-Separated Data
Parsing Fixed-Width Delimited Data
Taking Strings Apart
Wrapping Text at a Certain Line Length
Storing Binary Data in Strings

1.1. Introduction

Strings in PHP are a sequence of characters, such as "We hold these truths to be self evident," or "Once upon a time," or even "111211211." When you read data from a file or output it to a web browser, your data is represented as strings.

Individual characters in strings can be referenced with array subscript style notation, as in C. The first character in the string is at index 0. For example:

$neighbor = 'Hilda';
print $neighbor[3];
d

However, PHP strings differ from C strings in that they are binary-safe (i.e., they can contain null bytes) and can grow and shrink on demand. Their size is limited only by the amount of memory that is available.

You can initialize strings three ways, similar in form and behavior to Perl and the Unix shell: with single quotes, with double quotes, and with the "here document" (heredoc) format. With single-quoted strings, the only special characters you need to escape inside a string are backslash and the single quote itself:

print 'I have gone to the store.';
print 'I\'ve gone to the store.';
print 'Would you pay $1.75 for 8 ounces of tap water?';
print 'In double-quoted strings, newline is represented by \n';
I have gone to the store.
I've gone to the store.
Would you pay $1.75 for 8 ounces of tap water?
In double-quoted strings, newline is represented by \n

Because PHP doesn't check for variable interpolation or almost any escape sequences in single-quoted strings, defining strings this way is straightforward and fast.

Double-quoted strings don't recognize escaped single quotes, but they do recognize interpolated variables and the escape sequences shown in Table 1-1.

Table 1-1. Double-quoted string escape sequences

Escape sequence

Character

\n

Newline (ASCII 10)

\r

Carriage return (ASCII 13)

\t

Tab (ASCII 9)

\\

Backslash

\$

Dollar sign

\"

Double quotes

\{

Left brace

\}

Right brace

\[

Left bracket

\]

Right bracket

\0 through \777

Octal value

\x0 through \xFF

Hex value

For example:

print "I've gone to the store.";
print "The sauce cost \$10.25.";
$cost = '$10.25';
print "The sauce cost $cost.";
print "The sauce cost \$\061\060.\x32\x35.";
I've gone to the store.
The sauce cost $10.25.
The sauce cost $10.25.
The sauce cost $10.25.

The last line of code prints the price of sauce correctly because the character 1 is ASCII code 49 decimal and 061 octal. Character 0 is ASCII 48 decimal and 060 octal; 2 is ASCII 50 decimal and 32 hex; and 5 is ASCII 53 decimal and 35 hex.

Heredoc-specified strings recognize all the interpolations and escapes of double- quoted strings, but they don't require double quotes to be escaped. Heredocs start with <<< and a token. That token (with no leading or trailing whitespace), followed by a semicolon to end the statement (if necessary), ends the heredoc. For example:

print <<< END
It's funny when signs say things like:
   Original "Root" Beer
   "Free" Gift
   Shoes cleaned while "you" wait
or have other misquoted words.
END;
It's funny when signs say things like:
   Original "Root" Beer
   "Free" Gift
   Shoes cleaned while "you" wait
or have other misquoted words.

With heredocs, newlines, spacing, and quotes are all preserved. The end-of-string identifier is usually all caps, by convention, and it is case sensitive. Thus, this is okay:

print <<< PARSLEY
It's easy to grow fresh:
Parsley
Chives
on your windowsill
PARSLEY;

So is this:

print <<< DOGS
If you like pets, yell out:
DOGS AND CATS ARE GREAT!
DOGS;

Heredocs are useful for printing out HTML with interpolated variables:

if ($remaining_cards > 0) {
    $url = '/deal.php';
    $text = 'Deal More Cards';
} else {
    $url = '/new-game.php';
    $text = 'Start a New Game';
}
print <<< HTML
There are <b>$remaining_cards</b> left.
<p>
<a href="$url">$text</a>
HTML;

In this case, the semicolon needs to go after the end-of-string delimiter, to tell PHP the statement is ended. In some cases, however, you shouldn't use the semicolon:

$a = <<< END
Once upon a time, there was a
END
. ' boy!';
print $a;
Once upon a time, there was a boy!

In this case, the expression needs to continue on the next line, so you don't use a semicolon. Note also that in order for PHP to recognize the end-of-string delimiter, the . string concatenation operator needs to go on a separate line from the end-of-string delimiter.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.