Matching and Replacing Patterns

I l @ ve RuBoard

While the ereg() and eregi () functions are great for validating a string, you can take your programming one step further by matching a pattern and then replacing it with a slightly different pattern or with specific text. The syntax for using the two functions is:

 ereg_replace("pattern", "replace", "string"); 

Or:

 $Pattern = "pattern"; $Replace = "replace"; $String = "string"; eregi_replace($Pattern, $Replace, $String); 

One reason you might want to use this would be to turn a user -entered Web site address (a URL) into a clickable HTML link, by encapsulating it in the <A HREF="URL"></A> tags. I'll modify the form.html (Script 8.1) and HandleForm.php (Script 8.3) scripts to do just that.

To use eregi_replace to match and replace a pattern:

  1. Open form.html in your text editor. You'll slightly alter the form.html page to accept a URL and a description.

  2. Change line 9 (Script 8.4) (which takes the email address) to:

    Script 8.4. I have slightly modified the form.html page in order to have it accept URL submissions and descriptions instead of email addresses and comments.

    graphics/08sc04.jpg

     URL <INPUT TYPE=TEXT NAME=    "Array[URL]" SIZE=60><BR> 
  3. Change line 10, the comments, to:

     Description <TEXTAREA NAME=    "Array[Description]" ROWS=5    COLS=40></TEXTAREA><BR> 
  4. Save your script and upload it to the server.

    Now you'll rework the HandleForm.php page.

  5. Open HandleForm.php in your text editor (Script 8.3).

  6. Replace line 12 with the following code (Script 8.5):

    Script 8.5. The eregi_replace() function will turn the user-submitted URL into a hot clickable link automatically. This is possible largely due to back referencing.

    graphics/08sc05.jpg

  7.  $Pattern = "(http://)?([^[:space:]]+)    ([[:alnum:]\.,-_?/&=])"; 

    Here is a liberal pattern set to recognize a URL. In fact, this pattern is more adept for matching and replacing purposes than validating entered data, as it is not very restrictive .

    The pattern contains three groupings: the http://, the bulk of the URL, and the trailing part of the URL. A URL may begin with a http:// or not. To check this, you start with the http followed by the colon and two slashes , and use the question mark to indicate that the entire section is optional.

    After that, a URL contains characters that aren't spaces (the second, and very liberal, grouping) letters , numbers , dashes, underscores, periods, etc. Finally, allow for more alpha-numeric characters, as well as the slash, question mark, ampersand, and equals sign. This final parenthetical would match the end of a URL such as .com/php/.

  8.  $Replace = "<a href=\"http://    \2\3\" target=\"_new\">    \2\3</a>"; 

    Here you've defined the replacement text. Using the back referencing technique, you can grab a sub-section of a string and place it within another string. Since the http:// section is optional, the easiest way to insure consistency is to drop it during replacement (notice there is no \\1 reference) and then manually insert the http:// so that all URLs will display with the same formatting. After making this adjustment, and adding the <a href= tag, the remaining part of the URL is inserted, followed by the closing HTML tag ( </a> ).

  9.  $Array["URL"] = eregi_replace    ($Pattern, $Replace,    $Array["URL"]); 

    You reset the value of the URL to the new, replaced string. Now, whenever the URL is sent to the browser (as in the next line), it will appear as a clickable link. This altered form could also be stored in a database or file for later use.

  10.  print ("Your submission$Array[URL]    has been received!<BR>\n"); 

    Lastly, you conclude the if statement.

Save your script, upload it to the server, and test it in your Web browser (Figures 8.5, 8.6, and 8.7). If the script works as intended, it will take a user-submitted URL, check it for validity, and then turn it into a clickable link. This is a process that you will most likely be able to use in any number of Web applications.

Figure 8.5. The form.html page now requires a URL and description which will be checked using regular expressions (Figure 8.6).

graphics/08fig05.gif

Figure 8.6. With the eregi_replace() function, you are able to turn a submitted URL, with or without the http:// prefix, into a clickable link.

graphics/08fig06.gif

Figure 8.7. The source of the page reveals the <A HREF> tags which have been added to the user entered URL.

graphics/08fig07.jpg

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