PHP Tutorials

String Manipulation

Many Web sites ask for a visitor’s e-mail address when he wishes to communicate with the site; obviously, this gives the site administrator a chance to respond to questions, comments, and problems. But what happens if the user mistypes his e-mail address? The site administrator has no way to respond, which may make the visitor angry because, without a response, it may appear that the person or company behind the site doesn’t care.

E-mail address verification is one of the several types of string manipulation this chapter covers. Other types include joining strings, dissecting strings, and string replacements.

This chapter teaches you the following:

How to join strings

How to split strings

How to extract a substring from a string

How to use numeric string indexes

How to replace a string within a string

How to use regular expressions for complex string manipulation

Filed under: Chapter 5 @ 6:56 pm

Before We Begin

Being able to use string manipulation functions in your programs is similar to being able to make your program know how to read. For example, I already mentioned that by the end of this chapter you will be able to make your programs differentiate between valid and invalid e-mail addresses.

Although this chapter will cover some of the string manipulation functions, there are many more refined functions that are simply too numerous to cover for practical, day-to-day use.

However, some situations do lend themselves to these more refined functions. So, if you find yourself wishing PHP had a function to do something, take a look at the manual—there’s a function for just about anything you could need. And, as you might expect, the more you use PHP, the more of these functions will become familiar to you. For now, though, the functions this chapter teaches will give you a strong start.

Filed under: Chapter 5 @ 6:57 pm

The String Concatenation Operator

Like the numeric variable types, strings can be used with operators. However, operators don’t treat strings the same way they treat numeric variables. For example, multiplying two strings together wouldn’t make much sense. Neither would dividing, adding, or subtracting them.

TIP

You may be thinking, “Hey, wait. Wouldn’t adding strings ‘glue’ them together to form a composite?” This operation of “adding” strings, though, is known among programmers as concatenation, which has a separate operator. We’ll discuss that a little later in this section.

In fact, if you perform a mathematical operation on strings, the strings are typecast to the most appropriate numeric type before the expression evaluates them.

To refresh your memory on string-to-numeric typecasting, see “Type Casting,”.

The string concatenation operator, however, is made to work with strings. Concatenation is the joining together of two or more strings to form a single string. The string concatenation operator is the period; so, to join two strings together, we place a string on either side of a period. The resulting string can either be stored in a third string by assignment, or echoed directly to output.

NOTE

This works the same way as adding 1 + 2 + 3. You can have any number of concatenation operators in an expression, as long as each has a string on either side.

For example, if you ask a user for his first and last names as two separate entries, then you can use concatenation to display his full name in whatever format you prefer, or even switch back and forth within the same script as necessary.

The following program asks the user for his first and last names, then prints his full name in two different formats:



<?php
/* File: ch05ex01.php – demonstrates string concatenation */

?>
<html>
<head><title>PHP By Example :: Chapter 5 :: Example 1</title></head>

<body>

<?php

if ($HTTP_GET_VARS['fname'] && $HTTP_GET_VARS['lname'])
{
echo $HTTP_GET_VARS['fname'] . ' ' . $HTTP_GET_VARS['lname'] . '
';
echo $HTTP_GET_VARS['lname'] . ', ' . $HTTP_GET_VARS['fname'] . '
';
}

?>

<form action="ch05ex01.php" method="GET">
First Name: <input type="text" name="fname">

Last Name: <input type="text" name="lname">

<input type="submit">
</form>

</body>

</html>

Being able to join two strings in this manner expands the flexibility of your program. Because you know that this is possible, you can ask for first and last names separately, and, in turn, have the ability to manipulate that name however you want.

For example, let’s say you’re organizing a convention (such as the annual ApacheCon) for which participants will sign up through your Web site. By gathering people’s first and last names independently of each other (instead of gathering one, inflexible fullname), you have the ability to format and sort the names however you want.

In some instances—creating a roster of attendees, for example—you might want to list the names alphabetically by last name. To do so, you simply concatenate the last name, a string containing a comma and a space, and the first name, like so:

$name_full = $name_last . ', ' . $name_first;

The resulting string might look like this:

Smith, John

Later in this chapter, you will learn the skills needed to create a string containing a person’s initials using the strings containing his first and last names. (For example, given that a person’s first name is Joe and his last name is Smith, you could find that his initials are J.S.) For now, you should have a clear idea of how string concatenation works and how to do it.

Filed under: Chapter 5 @ 6:58 pm
Next Page »

Powered by WordPress