PHP for the World Wide Web (Visual QuickStart Guide)

I l @ ve RuBoard

Either because a user entered information carelessly or because of sloppy HTML code, it's quite common for extra spaces to be added to a string variable. For purposes of clarity, data integrity, and Web design, it is worth your while to delete those spaces from the strings before using them. Extra spaces sent to the Web browser could make the page appear oddly and those sent to a database or cookie could have unfortunate consequences at a later date (e.g., if a password has a superfluous space it might not match when entered without the space).

Script 5.1. The original HandleForm.php is quite simple and therefore lacks some extra measures for manipulating the received data.

The trim() function automatically strips away any extra spaces from both the front and the end of a string (but not within the middle). The format for using trim() is:

$String = " extra space before and after text "; $String = trim($String); // $String is now equal to "extra space before and after text".

Script 5.2. Besides trimming the extra white spaces from all the received data, I have also changed the title of our page, although it is not critical that you do so.

You'll use the HandleForm.php script (Script 3.4) from Chapter 3, HTML Forms and PHP, and modify it with this in mind.

To trim your strings:

  1. Open HandleForm.php in your text editor (Script 5.1).

  2. In Script 5.1, after line 6 (the comment), add the following lines (Script 5.2):

    $FirstName = trim($FirstName);

    By trimming the $FirstName variable you avoid sending a statement to the browser such as "Your first name is Larry." that contains extraneous spaces (Figures 5.1 and 5.2).

    Figure 5.1. The superfluous spaces here are subtle, but indicate a lack of foresight on the part of the Web developer. Figure 5.2, the HTML source of this image, is more revealing on this point.

    Figure 5.2. By viewing the HTML source, you are better able to see the extraneous white spaces that have been passed on to the form. While not critical here, sometimes an unnecessary blank line can really destroy the HTML design of a page.

  3. $LastName = trim($LastName); $Email = trim($Email);

    Trimming the email address would be particularly useful, as added spaces could render the address unusable for sending emails, thus possibly defeating the purpose of requesting it.

  4. $Comments = trim($Comments);

    Regardless of whether the string is drawn from a short piece of text (like the $FirstName or $Email variable) derived from an HTML text box, or a paragraph of writing from a text area, trim() will work the same.

  5. Save your script (still as HandleForm.php ), upload it to your server, then test in your Web browser (Figures 5.3, 5.4, and 5.5).

    Figure 5.3. For any number of reasons, it is pretty easy for the average user to add extra white spaces when entering values in a form. Preparing for this possibility is smart programming.

    Figure 5.4. Good programming frequently comes down to adjusting for the mistakes users may make, intentionally or not. Now your page has a slightly higher level of professionalism since you trimmed the user input.

    Figure 5.5. Compare the HTML source of Figure 5.3 here to that of Figure 5.2 and you'll easily see the differences that the trim() function made.

Tip

If you need to trim off excess spaces from the beginning or the end of a string, but not both, PHP has broken the trim() function down into two more specific ones: rtrim () will remove those spaces found at the end of a string variable and ltrim () will handle those at the beginning. They are both used just like trim():

$String = rtrim($String); $String = ltrim($String);

I l @ ve RuBoard

Категории