handling warnings, php5?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sergei Shelukhin

    #1

    handling warnings, php5?

    Hi.
    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?

  • Schraalhans Keukenmeester

    #2
    Re: handling warnings, php5?

    At Sun, 10 Jun 2007 05:36:11 -0700, Sergei Shelukhin let h(is|er) monkeys
    type:
    Hi.
    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?
    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
    details)

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

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

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

    Comment

    • gosha bine

      #3
      Re: handling warnings, php5?

      On 10.06.2007 14:36 Sergei Shelukhin wrote:
      Hi.
      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?
      >
      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($reg ex, ...)
      } catch(ErrorExce ption $e) {
      echo "Bad regex: $regex";
      }

      --
      gosha bine

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

      Comment

      Working...