handling warnings, php5?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I need to handle warnings in incorrect regular expressions executed
using preg_match.
Warnings shouldn't appear, instead I want to output some generic
message like: "Bad regex: $regex" and also do a database change.
I tried using try-catch but warnings are not caught.
What do I do?

Re: handling warnings, php5?

At Sun, 10 Jun 2007 05:36:11 -0700, Sergei Shelukhin let h(is|er) monkeys

Quoted text here. Click to load it

You can suppress warnings by setting the proper error_reporting() level
(see manual), or by prepending your function call with @.  

Then check for the returned value by the regex function and use
trigger_error('Your message',E_USER_WARNING). (Again, see manual for

if (! @preg_match($pattern,$string,$match_arr) {
  trigger_error ('Blahblah', E_USER_WARNING);

Schraalhans Keukenmeester - schraalhans@the.Spamtrapexample.nl
[Remove the lowercase part of Spamtrap to send me a message]

  "strcmp('apples','oranges') < 0"

Re: handling warnings, php5?

On 10.06.2007 14:36 Sergei Shelukhin wrote:
Quoted text here. Click to load it

The "natural" way is to use exceptions. First, install generic  
errors-to-exceptions handler (see Exceptions man page). Then, just  
enclose preg code in a try-catch block:

try {
    preg_match($regex, ...)
} catch(ErrorException $e) {
    echo "Bad regex: $regex";

gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok

Site Timeline