Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143
This is the forum to discuss the Wrox book Beginning PHP 6, Apache, MySQL 6 Web Development by Timothy Boronczyk, Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz; ISBN: 9780470391143
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 17th, 2009, 01:50 AM
Registered User
 
Join Date: Apr 2009
Posts: 8
Thanks: 0
Thanked 1 Time in 1 Post
Question Regular Expression Tester

I have been working through Chapter 8 and wanted to build Regular Expression tester. I wrote the code below and it works well provided there are no backslashes "\" in the expression or test string. I'm a bit of a newbie and it appears like the problem stems from the either the HTML form's treatment, or PHP's treatment, of the escape character; for some reason the form feeds \\ when ever \ is posted by the form. Is there a way to avoid this so that I can use the regular expression shorthand?

Here is the code I am using:

PHP Code:
<?php
 
// request Reg Exp if none has been provided
 
if (!isset($_GET['exp']) || !isset($_GET['test'])) {
     
$result 'Please Enter an Expression and a Test String';
 } else {
//test Reg Exp on String and return results
     
extract($_GET);
     if (!
preg_match('|' trim($exp) . '|',$test)){
         
$result 'The test does not meet the expression criteria';
     } else {
         
$result 'This test meets the expression criteria';
     }
 }

 
?>


<html>
 <head>
  <title>Regular Expression Checker</title>
 </head>
 <body>
  <h1>Test Your Regular Expression<h1/>
  <h2> Input you expression and test string:<br/><br/></h2>
  <form action="regexp.php" method="get">
   <table>
    <tr>
    <td>Regular Expression:</td>
    <td><input type="text" name="exp" value="<?php echo (isset($_GET['exp'])) ?  trim($_GET['exp']) : ''?>" /></td>
    <td><small>Use preg_match() expression mechanisms;<br/> there is no need to use delimiting characters</small></td>
    </tr><tr>
    <td>Test</td>
    <td><input type="text" name="test" value="<?php echo (isset($_GET['test'])) ? $_GET['test'] : ''?>" /></td>
    <td><input type="submit" name="submit" value="Test"></td>
    </table>
   </form>
    <br/><br/>
    <h2>Results:</h2>
    <p> <?php echo $result?></p>
 </body>
</html>
Reply With Quote
  #2 (permalink)  
Old April 17th, 2009, 05:08 PM
Registered User
 
Join Date: Apr 2009
Posts: 8
Thanks: 0
Thanked 1 Time in 1 Post
Default Is there anybody out there...

It looks like Im the only one on this forum... its become a diary of sorts. Ok, after spending hours searching the web for help I finally came across the function to fix my problem... the stripslashes() function will pull out the slashes added as escapes. So, some working code if anyone is interested:

PHP Code:
<?php
 
// request Reg Exp if none has been provided
 
if (!isset($_GET['exp']) || !isset($_GET['test'])) {
     
$result 'Please Enter an Expression and a Test String';
 } else {
//test Reg Exp on String and return results
     
extract($_GET);
    
     if (!
preg_match('|' stripslashes(trim($exp)) . '|',$test)){
         
$result 'The test does not meet the expression criteria';
     } else {
         
$result 'This test meets the expression criteria';
     }
 }

 
?>


<html>
 <head>
  <title>Regular Expression Checker</title>
 </head>
 <body>
  <h1>Test Your Regular Expression<h1/>
  <h2> Input you expression and test string:<br/><br/></h2>
  <form action="regexp.php" method="get">
   <table>
    <tr>
    <td>Regular Expression:</td>
    <td><input type="text" name="exp" value="<?php echo (isset($_GET['exp'])) ?  stripslashes(trim($_GET['exp'])) : ''?>" /></td>
    <td><small>Use preg_match() expression mechanisms;<br/> there is no need to use delimiting characters</small></td>
    </tr><tr>
    <td>Test</td>
    <td><input type="text" name="test" value="<?php echo (isset($_GET['test'])) ? stripslashes($_GET['test']) : ''?>" /></td>
    <td><input type="submit" name="submit" value="Test"></td>
    </table>
   </form>
    <br/><br/>
    <h2>Results:</h2>
    <p> <?php echo $result?></p>
 </body>
</html>
There are more robust expression checkers out there... but this works in a pinch.
Reply With Quote
  #3 (permalink)  
Old May 10th, 2009, 11:35 AM
Registered User
 
Join Date: May 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Someone is listening

Quote:
Originally Posted by sgtwwilson View Post
It looks like Im the only one on this forum... its become a diary of sorts.
Hey Sgt Wilson. I read your posts, though I may not be of much help at this time, as I truly am a beginner. I do have some experience with Perl, so am not a total newb regarding programming.
Anyway, thanks for the posts, and the code fixes. People like me need people like you to work through this stuff. I really need to learn this language. Keep up the good work!
Pooch
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expression Help pallone Javascript 2 October 19th, 2007 03:59 PM
What will be the regular expression for following? amit_mande@yahoo.com ASP.NET 1.0 and 1.1 Basics 2 December 26th, 2006 06:49 AM
Regular Expression DARSIN General .NET 2 November 9th, 2004 08:30 AM
Regular Expression Help Hadware .NET Web Services 3 November 4th, 2003 10:51 AM
help in regular expression diby Beginning PHP 1 September 17th, 2003 12:25 PM



All times are GMT -4. The time now is 03:54 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.