Practice 11

Main | Arrays | Practice 8 | Solution 8 | Associative Arrays | Practice 9 | Solution 9 | Regular Expressions | Practice 10 | Solution 10 | More Regular Expressions | Practice 11 | Solution 11

For this practice we will use the preg_split function to do the following:

  1. Split a sentence into a collection of words.

    For example:

    $sen = "This is a sentence";
    

    Could be processed into an array such that:

    $a[0] = "This";
    $a[1] = "is";
    $a[2] = "a";
    $a[3] = "sentence";
    
  2. Split a sentence into a collection of characters.

    For example:

    $sen = "This is a sentence";
    

    Could be processed into an array such that:

    $a[0] = "T";
    $a[1] = "h";
    $a[2] = "i";
    $a[3] = "s";
    $a[4] = " ";
    $a[5] = "i";
    ...and so on...
    
  3. Extract all of the arithmetic operators out of a mathematical expression.

    For example:

    $exp = "3 + 5 * 9 / 2";
    

    Could be processed into an array such that:

    $a[0] = "3";
    $a[1] = "5";
    $a[2] = "9";
    $a[3] = "2";
    
  4. Separate the paths of a directory.

    For example, the variable $PHP_SELF has the following value for this page:

     
    

    In the above the path to the file is specified with "/" to represent each sub-directory. Process the above such that:

    $a[0] = "~jfulton";
    $a[1] = "php3";
    $a[2] = "practice11.php";
    

Remember to escape any characters that you use which have special meaning by using the backslash.

To complete this exercise you must:

  • Create a file in "public_html/php" called "split.php".
  • Be sure that "split.php" is viewable from the web.
  • Fill in "split.php" with this incomplete code.

Your program should look like this when run.

HINT:
The Perl Compatible Regular Expression that allows you to represent whitespace (" ") is the following:

\s

The above symbol will be helpful for the first example.


jfulton [at] member.fsf.org
22 Aug 2013