preg_match - how to match \x00 (null)?

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

    preg_match - how to match \x00 (null)?

    Hi all,

    I'd like to work with a binary file....

    I read the whole file into variable (it conatains binary data from 00-FF):

    $fp=fopen($cfg, "r");
    $content=fread( $fp,filesize($c fg));
    fclose($fp);

    // and then I'd like to check if it contains special binary sequence, let's say bytes "00 01
    02 03":

    $replacestring= "/\x00\x01\x02\x0 3/im";

    if (preg_match($re placestring,$co ntent)==0) {
    echo("bad input file format");
    exit();
    }

    but i get error message:

    <b>Warning</b>: preg_match(): No ending delimiter '/' found in <b>test.bat</b> on
    line <b>57</b><br />

    I think it is maybe because php uses strings terminated with null, but can i force php to
    use strings with predefined length for example? Is there a way to work with strings
    containing null characters?

    Thank you in advance.

    Y.
  • Chung Leong

    #2
    Re: preg_match - how to match \x00 (null)?

    Yandos wrote:[color=blue]
    > Hi all,
    >
    > I'd like to work with a binary file....
    >
    > I read the whole file into variable (it conatains binary data from 00-FF):
    >
    > $fp=fopen($cfg, "r");
    > $content=fread( $fp,filesize($c fg));
    > fclose($fp);
    >
    > // and then I'd like to check if it contains special binary sequence, let's say bytes "00 01
    > 02 03":
    >
    > $replacestring= "/\x00\x01\x02\x0 3/im";
    >
    > if (preg_match($re placestring,$co ntent)==0) {
    > echo("bad input file format");
    > exit();
    > }
    >
    > but i get error message:
    >
    > <b>Warning</b>: preg_match(): No ending delimiter '/' found in <b>test.bat</b> on
    > line <b>57</b><br />
    >
    > I think it is maybe because php uses strings terminated with null, but can i force php to
    > use strings with predefined length for example? Is there a way to work with strings
    > containing null characters?
    >
    > Thank you in advance.
    >
    > Y.[/color]

    The PCRE library does expect the pattern to be a null-terminate string.
    It is capable of looking for \0 though. Instead of the double-quoted
    string, use single-quotes instead:

    $replacestring= '/\x00\x01\x02\x0 3/im';

    Now PCRE will see the escape sequences and correctly compile the regexp.

    Comment

    • Mladen Gogala

      #3
      Re: preg_match - how to match \x00 (null)?

      On Tue, 06 Jun 2006 14:23:24 -0700, Chung Leong wrote:
      [color=blue]
      > $replacestring= '/\x00\x01\x02\x0 3/im';[/color]

      Why do you use multi-line matching and what will ignoring case buy you
      here?

      --


      Comment

      • Yandos

        #4
        Re: preg_match - how to match \x00 (null)?

        "Chung Leong" <chernyshevsky@ hotmail.com> wrote in
        news:1149629004 .581297.79180@h 76g2000cwa.goog legroups.com:
        [color=blue]
        >
        > The PCRE library does expect the pattern to be a null-terminate
        > string. It is capable of looking for \0 though. Instead of the
        > double-quoted string, use single-quotes instead:
        >
        > $replacestring= '/\x00\x01\x02\x0 3/im';
        >
        > Now PCRE will see the escape sequences and correctly compile the
        > regexp.
        >[/color]

        Works great, thank you!

        Y.

        --
        sorry for any ads below :(

        Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php

        Comment

        • Yandos

          #5
          Re: preg_match - how to match \x00 (null)?

          Mladen Gogala <gogala@sbcglob al.net> wrote in news:pan.2006.0 6.07.07.58.37.7 53254
          @sbcglobal.net:
          [color=blue]
          > On Tue, 06 Jun 2006 14:23:24 -0700, Chung Leong wrote:
          >[color=green]
          >> $replacestring= '/\x00\x01\x02\x0 3/im';[/color]
          >
          > Why do you use multi-line matching and what will ignoring case buy you
          > here?
          >[/color]

          Thanks for comment, that's perhaps not necessary, but I'm used to do regexps that way. I'm not a
          regexp guru and once something did not worked when I have not used ignore case and multiline
          switches, so now I'm using em everywhere - no harm in writing 2 more letters ;) )

          Y.

          --
          sorry for any ads below :(

          Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php

          Comment

          Working...