Hello all and thanks for reading my question.

I'm trying to make a mini online regex tester for testing my expressions


  $PostReg = trim( $_POST['regex'] );
  $PostTst = trim( $_POST['chars'] );

    IF ( isSet ( $_POST['regex'] ) ){
         IF ( !preg_match( $PostReg, $PostTst, $matches ) ){
              $Out = "No Errors in Posted Text <br />Chars = ". $PostTst . "<br />Within bounds of " . $PostReg;

         }ELSE{
              $Out = "Error in Posted Text match = ". $matches[0];

         }
    }

so I have a form that posts the data for $PostReg, $PostTst
The values are as follows in the actual form fields
$PostReg = /[0-9]{6}$/

$PostTst = 123556655856525m

it returns that No Errors in Posted Text even it I send an empty field for $PostTst it still says No Errors in Posted Text

Thanks for any help

    /[0-9]{1,12}/

    for some reason if I remove the $ off the end of /[0-9]{6}$/ so it reads /[0-9]{6}/
    it seems to work but says No Errors in Posted Text with 1234 when it should have 6 chars
    so I do
    /[0-9]{6,6}/
    and 111111111111111111 produces errors finally...lol
    but then I use
    111
    and we are back to no No Errors in Posted Text... i just don't understand

      It seems like you are confusing yourself with the notion of an "error". /[0-9]{6}$/ matches a string of 6 digits exactly. '123556655856525m' does not match the regex pattern, thus the first message is printed. This message happens to say "No Errors in Posted Text". Apparently, you regard a failure to match as an error, but then your script is programmed to state the opposite. A quick solution would be to remove the not (!), but that still does not entirely make sense, since you refer to $matches[0] in the other branch.

        Ok I rewrote it a little with your input I think its correct now...lol...sorry very confuse by the results I'm getting 😕

        <? 
        
        $self=$_SERVER['PHP_SELF']; # Get the Global
        
        if($_POST['pattern']) {
        	$pattern=$_POST['pattern'];
        	$string=$_POST['string'];
        	if (preg_match($pattern,$string))   //preg_match or  eregi
        		echo "$pattern pattern matches \"$string\"";
        	else
        		echo "$pattern pattern does not match \"$string\"";
        	}
        ?>
        <html>
        <body>
               <h1>Testing Regular Expressions</h1>
               <form action=<? echo "$self"; ?> method=POST>
                    String: <input type=text name="string" value="<? echo "$string"; ?>" size=30><br><br>
        
        RegEx Pattern: <input type=text name="pattern" value="<? echo "$pattern"; ?>" size=30><br><br>
        
        <input type=submit value="Test Pattern" name="Submit">
               </form>
        </body>
        </html>
        
        

        With preg_match
        /[0-9]{6}$/ pattern matches "123456"
        /[0-9]{6}$/ pattern does not match "12345"
        /[0-9]{6}$/ pattern does not match "1234567"
        now that makes sence....

        So I tried it with
        /[0-9]{2,12}$/ pattern matches "12"
        /[0-9]{2,12}$/ pattern does not match "1"
        /[0-9]{2,12}$/ pattern matches "123456789012"
        /[0-9]{2,12}$/ pattern does not match "1234567890123"
        All makes sence now :eek:

        Thanks for you help

          Write a Reply...