Transforming Between Strings and Arrays

I l @ ve RuBoard

Now that you understand both strings and arrays, I'll introduce two functions for switching between the two formats. One, implode(), turns an array into a string. The second, explode(), does just the opposite . Here are a some reasons to use these functions:

  • You might want to turn an array into a string in order to pass that value appended to a URL (which you cannot do as easily with an array).

  • You might want to turn an array into a string in order to store that information in a database.

  • You might want to turn a string into an array to convert a comma-delimited text field (say a keyword search area of a form) into its separate parts .

The syntax for using explode() is:

 $Array = explode ($Separator, $String); 

The separator refers to whatever character(s) define where one array element ends and another begins. Commonly this would be either a comma or a blank space. Thus your code would be: $Array = explode(",", $String);

Or:

 $Array = explode(" ", $String); 

To go from an array to a string, you need to define what the separator (a.k.a., the glue) is going to be and PHP does the rest.

 $String = implode($Glue, $Array); $String = implode(",", $Array); 

Or:

 $String = implode(" ", $Array); 

To demonstrate how to use explode() and implode(), you'll create an HTML form that takes a comma delimited string of names from the user . The PHP will then turn the string into an array so that it can sort the list. Finally the code will create and return the alphabetized string.

To convert between strings and arrays:

  1. Create a new HTML document in your text editor.

  2. Write the standard HTML header (Script 7.6):

     <HTML><HEAD><TITLE>HTML Form  </TITLE></HEAD><BODY> 
    Script 7.6. This is just a simple HTML form where a user can submit a list of words. Including detailed instructions for the user is a prudent Web design policy.

    graphics/07sc06.jpg

  3. Create an HTML form with a TEXT input.

     <FORM ACTION="HandleList.php"  METHOD=POST> Enter the words you want alphabetized  with each individual word separated  by a space:<BR> <INPUT TYPE=TEXT NAME="List"  SIZE=80><BR> 

    It's important in cases like this to instruct the user. For example, if they enter a comma delimited list, you won't be able to handle the string properly (after completing both scripts, try using commas in lieu of spaces and see what results).

  4. Create a submit button, then close the form and the HTML page.

     <INPUT TYPE=SUBMIT NAME="SUBMIT"  VALUE="Submit!">  </FORM></BODY></HTML> 
  5. Save your script as list.html and upload it to your server.

    Now you'll write the HandleList.php page to process the data generated by list.html.

  6. Create a new HTML document in your text editor.

  7. Write the standard HTML header and then open the PHP section of the page (Script 7.7):

     <HTML><HEAD><TITLE>Alphabetizing  Example</TITLE><BODY><?php 
    Script 7.7. Because the explode() and implode() functions are so simple and powerful, you can quickly and easily sort a submitted list of words (of practically any length) in just a couple of lines.

    graphics/07sc07.jpg

  8. Turn the incoming string, $List, into an array.

     $Array = explode (" ", $List); 

    This line of code creates a new array, $Array, out of the string $List. Each space between the words in $List indicates to set the next word as a new array element. Hence the first word becomes $Array[0], then there is a space in $List, then the second word becomes $Array[1], and so forth until the end of $List.

  9. Sort the array alphabetically .

     sort ($Array); 

    Because you do not need to maintain key-value associations in the $Array, you can use sort() instead of asort() which you used before.

  10. Create a new string out of the sorted array.

     $NewList = implode ("<BR>", $Array); 

    You'll print out the new list and since arrays do not print as easily as strings, you'll turn $Array into a string called $NewList first. The resulting string will start with the value of $Array[0], followed by the HTML <BR> tag, followed by the value of $Array[1], etc. Using <BR> instead of a space or comma will give the list a more readable format when printed to the browser.

  11. Print the new string to the browser.

     print ("An alphabetized version of  your list is:<BR>$NewList"); 
  12. Close your PHP section and the

     HTML page. ?></BODY></HTML> 
  13. Save your page as HandleList.php, upload it to the server (in the same directory as list.html ), and test both scripts in your Web browser (Figures 7.8 and 7.9).

    Figure 7.8. The HTML form here takes a list of words which will then be alphabetized in the HandleList.php script (Figure 7.9).

    graphics/07fig08.gif

    Figure 7.9. Here's the same list, alphabetized for the user. It's a quick and easy process to code but impossible without the existence of arrays.

    graphics/07fig09.gif

Tip

You'll also run across code written using the join() function, which is identical to implode().


I l @ ve RuBoard


PHP for the World Wide Web (Visual QuickStart Guide)
PHP for the World Wide Web (Visual QuickStart Guide)
ISBN: 0201727870
EAN: 2147483647
Year: 2001
Pages: 116
Authors: Larry Ullman

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