question about preg_*'s s modifer

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

    question about preg_*'s s modifer

    Say I have the following script:

    <?php
    $contents = file_get_conten ts('preg_test.t xt');
    echo preg_match("#(. *?)[\r\n]+ddddddddddddd# s",$contents ) ? 'is
    equal' : 'is not equal';
    ?>

    Here's preg_test.txt:

    Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


    (it's a malformed part of a postscript file, in case you're curious)

    My question is... when I remove the s modifier, preg_match returns
    true. When the s modifier is there, it returns false. I'm not really
    sure why this is. The s modifier means that . includes new lines and
    carriage returns. In either case, it seems like it should match.

    Any ideas as to why it doesn't?

  • Rik

    #2
    Re: question about preg_*'s s modifer

    yawnmoth <terra1024@yaho o.comwrote:
    Say I have the following script:
    >
    <?php
    $contents = file_get_conten ts('preg_test.t xt');
    echo preg_match("#(. *?)[\r\n]+ddddddddddddd# s",$contents ) ? 'is
    equal' : 'is not equal';
    ?>
    >
    Here's preg_test.txt:
    >
    Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!

    >
    (it's a malformed part of a postscript file, in case you're curious)
    >
    My question is... when I remove the s modifier, preg_match returns
    true. When the s modifier is there, it returns false. I'm not really
    sure why this is. The s modifier means that . includes new lines and
    carriage returns. In either case, it seems like it should match.
    Hmmmz, they both match here in PHP 5.1.4, PCRE Library Version 6.6
    06-Feb-2006, with or without the modifier. What version are you using?

    --
    Rik Wasmus

    Comment

    • yawnmoth

      #3
      Re: question about preg_*'s s modifer

      On Feb 5, 2:13 pm, Rik <luiheidsgoe... @hotmail.comwro te:
      yawnmoth<terra1 ...@yahoo.comwr ote:
      Say I have the following script:
      >
      <?php
      $contents = file_get_conten ts('preg_test.t xt');
      echo preg_match("#(. *?)[\r\n]+ddddddddddddd# s",$contents ) ? 'is
      equal' : 'is not equal';
      ?>
      >
      Here's preg_test.txt:
      >>
      (it's a malformed part of a postscript file, in case you're curious)
      >
      My question is... when I remove the s modifier, preg_match returns
      true. When the s modifier is there, it returns false. I'm not really
      sure why this is. The s modifier means that . includes new lines and
      carriage returns. In either case, it seems like it should match.
      >
      Hmmmz, they both match here in PHP 5.1.4, PCRE Library Version 6.6
      06-Feb-2006, with or without the modifier. What version are you using?
      PHP 5.2.0 (cli).

      Comment

      • Rik

        #4
        Re: question about preg_*'s s modifer

        yawnmoth <terra1024@yaho o.comwrote:
        On Feb 5, 2:13 pm, Rik <luiheidsgoe... @hotmail.comwro te:
        >yawnmoth<terra 1...@yahoo.comw rote:
        Say I have the following script:
        >>
        <?php
        $contents = file_get_conten ts('preg_test.t xt');
        echo preg_match("#(. *?)[\r\n]+ddddddddddddd# s",$contents ) ? 'is
        equal' : 'is not equal';
        ?>
        >>
        Here's preg_test.txt:
        >>>>
        (it's a malformed part of a postscript file, in case you're curious)
        >>
        My question is... when I remove the s modifier, preg_match returns
        true. When the s modifier is there, it returns false. I'm not really
        sure why this is. The s modifier means that . includes new lines and
        carriage returns. In either case, it seems like it should match.
        >>
        >Hmmmz, they both match here in PHP 5.1.4, PCRE Library Version 6.6
        >06-Feb-2006, with or without the modifier. What version are you using?
        >
        PHP 5.2.0 (cli).
        Hmmmz, the file you gave was the actual file you tested it with, or justa
        part of it? Might be it just maxes out. I really don't have any other
        ideas :(
        --
        Rik Wasmus

        Comment

        • Curtis

          #5
          Re: question about preg_*'s s modifer

          On Mon, 05 Feb 2007 11:24:30 -0800, yawnmoth <terra1024@yaho o.comwrote:
          Say I have the following script:
          >
          <?php
          $contents = file_get_conten ts('preg_test.t xt');
          echo preg_match("#(. *?)[\r\n]+ddddddddddddd# s",$contents ) ? 'is
          equal' : 'is not equal';
          ?>
          >
          Here's preg_test.txt:
          >
          Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!

          >
          (it's a malformed part of a postscript file, in case you're curious)
          >
          My question is... when I remove the s modifier, preg_match returns
          true. When the s modifier is there, it returns false. I'm not really
          sure why this is. The s modifier means that . includes new lines and
          carriage returns. In either case, it seems like it should match.
          >
          Any ideas as to why it doesn't?
          >
          This is a very inefficient regex for a large amount of data. Since you are
          using the lazy asterisk with the dot, the regex engine immediately starts
          backtracking throughout the search. It would be easier to specify the
          amount of d's through the {} quantifier, not hardcoding.

          Is there a reason you capture all the content before the CRLF and d
          portion of the pattern? It looks like you're merely testing if any
          whitespace and 13 d's exist. If that's the case, you could just use the
          strstr() function. If you want everything except the whitespace and d's,
          then use substr().

          --
          Curtis, http://dyersweb.com

          Comment

          • yawnmoth

            #6
            Re: question about preg_*'s s modifer

            On Feb 5, 4:03 pm, Rik <luiheidsgoe... @hotmail.comwro te:
            yawnmoth<terra1 ...@yahoo.comwr ote:
            On Feb 5, 2:13 pm, Rik <luiheidsgoe... @hotmail.comwro te:
            <snip>
            Hmmmz, the file you gave was the actual file you tested it with, or just a
            part of it? Might be it just maxes out. I really don't have any other
            ideas :(
            Yup - that's the actual file. In testing similar files, I noted that
            removing a few characters from the middle made it work just fine.
            This made me think that it was maxing out, but then I tried to write a
            PHP script to sorta auto-generate a more simplified file that'd
            demonstrate the problem but was unable to do so.

            Comment

            • yawnmoth

              #7
              Re: question about preg_*'s s modifer

              On Feb 7, 2:50 am, Curtis <dyers...@veriz on.netwrote:
              On Mon, 05 Feb 2007 11:24:30 -0800,yawnmoth<t erra1...@yahoo. comwrote:
              Say I have the following script:
              >
              <?php
              $contents = file_get_conten ts('preg_test.t xt');
              echo preg_match("#(. *?)[\r\n]+ddddddddddddd# s",$contents ) ? 'is
              equal' : 'is not equal';
              ?>
              >
              Here's preg_test.txt:
              >>
              (it's a malformed part of a postscript file, in case you're curious)
              >
              My question is... when I remove the s modifier, preg_match returns
              true. When the s modifier is there, it returns false. I'm not really
              sure why this is. The s modifier means that . includes new lines and
              carriage returns. In either case, it seems like it should match.
              >
              Any ideas as to why it doesn't?
              >
              This is a very inefficient regex for a large amount of data. Since you are
              using the lazy asterisk with the dot, the regex engine immediately starts
              backtracking throughout the search. It would be easier to specify the
              amount of d's through the {} quantifier, not hardcoding.
              >
              Is there a reason you capture all the content before the CRLF and d
              portion of the pattern? It looks like you're merely testing if any
              whitespace and 13 d's exist. If that's the case, you could just use the
              strstr() function. If you want everything except the whitespace and d's,
              then use substr().
              I'm trying to extract fonts from *.ps files. Because the fonts can
              have any name of any length (afaik), substr() isn't sufficient. That
              said, I assume [^\r\n]+ would be more efficient than .*? ?

              Comment

              • Curtis

                #8
                Re: question about preg_*'s s modifer

                yawnmoth wrote:
                On Feb 7, 2:50 am, Curtis <dyers...@veriz on.netwrote:
                >On Mon, 05 Feb 2007 11:24:30 -0800,yawnmoth<t erra1...@yahoo. comwrote:
                >>Say I have the following script:
                >><?php
                >>$contents = file_get_conten ts('preg_test.t xt');
                >>echo preg_match("#(. *?)[\r\n]+ddddddddddddd# s",$contents ) ? 'is
                >>equal' : 'is not equal';
                >>?>
                >>Here's preg_test.txt:
                >>http://www.geocities.com/terra1024/preg_test.txt
                >>(it's a malformed part of a postscript file, in case you're curious)
                >>My question is... when I remove the s modifier, preg_match returns
                >>true. When the s modifier is there, it returns false. I'm not really
                >>sure why this is. The s modifier means that . includes new lines and
                >>carriage returns. In either case, it seems like it should match.
                >>Any ideas as to why it doesn't?
                >This is a very inefficient regex for a large amount of data. Since you are
                >using the lazy asterisk with the dot, the regex engine immediately starts
                >backtracking throughout the search. It would be easier to specify the
                >amount of d's through the {} quantifier, not hardcoding.
                >>
                >Is there a reason you capture all the content before the CRLF and d
                >portion of the pattern? It looks like you're merely testing if any
                >whitespace and 13 d's exist. If that's the case, you could just use the
                >strstr() function. If you want everything except the whitespace and d's,
                >then use substr().
                I'm trying to extract fonts from *.ps files. Because the fonts can
                have any name of any length (afaik), substr() isn't sufficient. That
                said, I assume [^\r\n]+ would be more efficient than .*? ?
                >
                Yeah [^\r\n]+ is definitely more efficient, as it won't cause
                backtracking.

                --
                Curtis, http://dyersweb.com

                Comment

                Working...