Team LiB
Previous Section Next Section

Working with String Functions

Numerous string functions are built into PHP, all of which are designed to make your life easier. Suppose you have to normalize strings for news headlines or product ID numbers, or calculate the length of a string before trying to stuff it into a database field. Those are just a few of the string functions you'll learn about in the next section. For more string functions, take a look at the PHP manual at http://www.php.net/strings. The function list grows daily as more people contribute to the language.

Creating an Input Form

In this section, you'll create the front end to a string modification script. This form will contain one text area and several radio buttons. The radio buttons will determine the string function to use.

  1. Open a new file in your text editor and type the following HTML:

    <HTML>
    <HEAD>
    <TITLE>Generic Input Form</TITLE>
    </HEAD>
    <BODY>
    
  2. Begin your form. Assume that the method is POST and the action is a script called display_input.php:

    <FORM METHOD="post" ACTION="display_input.php">
    
  3. Create a text area with a text label:

    <P><strong>Text Field:</strong><br>
    <TEXTAREA NAME="text1" COLS=45 ROWS=5 WRAP=virtual></TEXTAREA></P>
    
  4. Add this block of radio buttons:

    <P><strong>String Function:</strong><br>
    <INPUT TYPE="radio" NAME="func" VALUE="md5"> get md5<br>
    <INPUT TYPE="radio" NAME="func" VALUE="strlen"> get length of string<br>
    <INPUT TYPE="radio" NAME="func" VALUE="strrev"> reverse the string<br>
    <INPUT TYPE="radio" NAME="func" VALUE="strtoupper"> make string uppercase<br>
    <INPUT TYPE="radio" NAME="func" VALUE="strtolower"> make string lowercase<br>
    <INPUT TYPE="radio" NAME="func" VALUE="ucwords"> make first letter of all
                                                          words uppercase</P>
    
    
    
    Note 

    The value for each radio button is its exact PHP function name. This will make the back-end script very simple to create, as you'll see in the next section.

  5. Add a submit button:

    <P><INPUT TYPE="submit" NAME="submit" VALUE="Do Something With the
    String"></P>
    
  6. Close your form and add some more HTML so that the document is valid:

    </FORM>
    </BODY>
    </HTML>
    
  7. Save the file with the name generic_form.html, and place this file in the document root of your web server.

  8. Open your web browser and type http://127.0.0.1/generic_form.html.

    Click To expand

You'll see a form with a text area and several radio buttons, along with a Do Something With the String form submission button. In the next section, you'll create the back-end script. That script will expect two variables: $_POST[text1] and $_POST[func].

Creating a Script to Display Form Values

According to the form action in generic_form.html, you need a script called display_input.php. The goal of this script is to accept the text in $_POST[text1] and use a particular string function (the value of $_POST[func]) to get a new result ($result).

  1. Open a new file in your text editor and type the following PHP code. This will ensure that the user is sent back to the form if no value is entered in the text area and no function is selected from the list of radio buttons:

    <?
    if (($_POST[text1] == "") || ($_POST[func] == "")) {
         header("Location: generic_form.html");
         exit;
    }
    
  2. Type the next bit of PHP, which assigns the value of the function output to a variable called $result, and then close the PHP block:

    $result = $_POST[func]($_POST[text1]);
    ?>
    
  3. Start the HTML output:

    <HTML>
    <HEAD>
    <TITLE>Generic Input Results</TITLE>
    </HEAD>
    <BODY>
    
  4. Display the value of $result:

    <? echo "$result"; ?>
    
  5. Add a link back to the form:

    <P><a href="generic_form.html">Go again!</a></P>
    
  6. Add some more HTML so that the document is valid:

    </BODY>
    </HTML>
    
    
  7. Save the file with the name display_input.php, and place this file in the document root of your web server.

    Your code should look something like this:

    <?
    if (($_POST[func] == "") || ($_POST[text1] == "")) {
    
         header("Location: generic_form.html");
         exit;
    }
    $result = $_POST[func]($_POST[text1]);
    ?>
    <HTML>
    <HEAD>
    <TITLE>Generic Input Results</TITLE>
    </HEAD>
    <BODY>
    <? echo "$result"; ?>
    <p><a href="generic_form.html">Go again!</a></p>
    </BODY>
    </HTML>
    

In the next section, you'll submit the form and see all these different types of string functions at work.

Submitting Your Form and Getting Results

Now that you've created both a front-end form and a back-end script, it's time to try them out.

  1. Open your web browser and type http://127.0.0.1/generic_form.html.

  2. Type the following text in the text area:

    I think PHP is just the coolest server-side scripting language around!
    Who knew it would be this simple?
    
    
    
    Note 

    You might want to copy that chunk of text to the clipboard, because it will be used in all of the following examples.

  3. Select the get md5 radio button, and click on the Do Something With the String button.

    Click To expand
Note 

The md5() function gets a hash of the string. A hash is like a digital summary of the string. It can be used to compare versions of strings (or files) to determine whether the versions differ.

You should see a hash of the string, along with a link back to the form. Return to the form and enter the same text, only this time select the button that will use the strlen() function to find the length of the string, including white space and all characters.

Click To expand

Continue testing each of the remaining functions. When you select the button to use the strrev() function, you will see that your original string has been completely reversed. The strtoupper() function returns the string with all letters in uppercase, whereas the strtolower() function returns the string with all letters in lowercase. Finally, use the ucwords() function to return the string with the first letter of each word in uppercase.

Click To expand

Team LiB
Previous Section Next Section