dbo Posted August 30, 2007 Share Posted August 30, 2007 I'm writing some code that allows users to enter in a regex string and then calls preg_match. Using preg_match to validate a regex string before calling preg_match just seems weird to me, so what I'd like to do is error handle preg_match... is there a way I could emulate functionality thats like: preg_match(..., ...) || return false; See I don't want to stop executing the script at all, just want to treat that function breaking as false and report an error message. I'm sure I could pull it off with a try/catch but that's not something that would work in PHP4. Soooo what other suggestion do you guys have? Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 30, 2007 Share Posted August 30, 2007 I don't understand the problem...why not do if(!preg_match(..., ...)){ print 'error'; } Quote Link to comment Share on other sites More sharing options...
dbo Posted August 30, 2007 Author Share Posted August 30, 2007 uh, that's a completely different issue. I'm talking about error handling preg match due to input that might break it. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted August 30, 2007 Share Posted August 30, 2007 I'm not 100% sure what your looking for.. but if you wish to check if the regex was valid then maybe use preg_last_error, example <?php $RegEx = "/\s/si"; $String = "testing"; $data = @preg_replace($RegEx, $String); switch(preg_last_error()) { case 0: //no error break; case 1: echo "PREG_INTERNAL_ERROR"; break; case 2: echo "PREG_BACKTRACK_LIMIT_ERROR"; break; case 3: echo "PREG_RECURSION_LIMIT_ERROR"; break; case 4: echo "PREG_BAD_UTF8_ERROR"; break; } ?> Quote Link to comment Share on other sites More sharing options...
dbo Posted August 30, 2007 Author Share Posted August 30, 2007 That's got potential. It seems like just in general there ought to be a way to do: func() || return false; Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 30, 2007 Share Posted August 30, 2007 "preg_match() returns FALSE if an error occurred." So, if(preg_match(your input) === false){ print 'error'; } You only use || inside a conditional statement, you don't just say do this or this. You say if this, do this, which can mean if this fails, do this. Same thing. Quote Link to comment Share on other sites More sharing options...
dbo Posted August 30, 2007 Author Share Posted August 30, 2007 Take the following code for example: $matches = preg_match("/balh", "10"); if( $matches != 0 ) echo "woot!"; Naturally ther errors out because I'm missing my closing / in the regex. However, it yields this error message: Warning: preg_match() [function.preg-match]: No ending delimiter '/' found in /var/www/test.php on line 20. Are you suggesting that I compress the error message with @ and it will return false? Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 30, 2007 Share Posted August 30, 2007 Well, try it and see Quote Link to comment Share on other sites More sharing options...
dbo Posted August 30, 2007 Author Share Posted August 30, 2007 Laugh, that seems like such a hack... but I think it will work. I still think i should be able to func() || return false! Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 30, 2007 Share Posted August 30, 2007 how can it return if it's not inside a function? If you really want it to return a value make a function. But you have to use the if else construct, like I said. It's the same thing, just with the PROPER statements. Quote Link to comment Share on other sites More sharing options...
dbo Posted August 30, 2007 Author Share Posted August 30, 2007 I could make it work with a global variable! lol Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.