Windows/ Unix regular expression question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • e_matthes@hotmail.com

    Windows/ Unix regular expression question

    Hello,

    I have a function which uses a regular expression to validate text
    input. Here's a short code sample testing the regex:

    <?php

    $dirty = "hello";
    $clean = getCleanText($d irty, 0,50);
    print "<br>dirty: $dirty<br>clean : $clean";

    function getCleanText($t extIn, $minLengthIn, $maxLengthIn) {

    $acceptableText RegExp = "/^[a-zA-Z0-9_\- ]\{$minLengthIn,
    $maxLengthIn}/";
    print "<br>$acceptabl eTextRegExp<br> ";

    if ( preg_match($acc eptableTextRegE xp, $textIn, $matchedArray) ) {
    return $matchedArray[0];
    } else {
    return "";
    }

    } // END getCleanText($t extIn, $minLengthIn, $maxLengthIn)

    ?>


    The output on my local windows machine is:

    /^[a-zA-Z0-9_\- ]{0,50}/
    dirty: hello
    clean: hello

    The output from my web site on a shared unix server is:

    /^[a-zA-Z0-9_\- ]\{0,50}/
    dirty: hello
    clean:

    If I take out the \ before the open brace, both systems give me an
    error about unmatched braces. Are escape characters handled
    differently on windows and unix? Why does the unix version leave the
    escape character in the reg ex string after parsing the string?

    Thanks.

  • Toby A Inkster

    #2
    Re: Windows/ Unix regular expression question

    e_matthes wrote:
    $acceptableText RegExp = "/^[a-zA-Z0-9_\- ]\{$minLengthIn, $maxLengthIn}/";
    $acceptableText RegExp = sprintf('/^[a-zA-Z0-9 _-]{%d,%d}$/',
    (int)$minLength In,
    (int)$maxLength In);


    --
    Toby A Inkster BSc (Hons) ARCS
    [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
    [OS: Linux 2.6.12-12mdksmp, up 95 days, 2:52.]

    Non-Intuitive Surnames

    Comment

    • Alexey Kulentsov

      #3
      Re: Windows/ Unix regular expression question

      e_matthes@hotma il.com wrote:
      $acceptableText RegExp = "/^[a-zA-Z0-9_\- ]\{$minLengthIn,
      $maxLengthIn}/";
      .....
      If I take out the \ before the open brace, both systems give me an
      error about unmatched braces. Are escape characters handled
      PHP try to parse this: {$minLengthIn,$ maxLengthIn} as one expression. {}
      used in PHP string as special symbols. You can use '-string to avoid
      such things:

      $acceptableText RegExp = '/^[a-zA-Z0-9_\-
      ]{'.$minLengthIn .'.'.$maxLength In.'}/';

      Comment

      • e_matthes@hotmail.com

        #4
        Re: Windows/ Unix regular expression question

        On May 29, 3:32 pm, Alexey Kulentsov <criman...@crim aniak.comwrote:
        e_matt...@hotma il.com wrote:
        $acceptableText RegExp = "/^[a-zA-Z0-9_\- ]\{$minLengthIn,
        $maxLengthIn}/";
        .....
        If I take out the \ before the open brace, both systems give me an
        error about unmatched braces. Are escape characters handled
        >
        PHP try to parse this: {$minLengthIn,$ maxLengthIn} as one expression. {}
        used in PHP string as special symbols. You can use '-string to avoid
        such things:
        >
        $acceptableText RegExp = '/^[a-zA-Z0-9_\-
        ]{'.$minLengthIn .'.'.$maxLength In.'}/';

        I can use the single-quote method to make my code work:
        $regExp = '/^[a-zA-Z0-9_\- ]{' . "$minLength,$ma xLength" . '}/';

        I would still really like to know, however, why my local php
        installation and the installation on my shared server interpret this
        line differently:
        $regExp = "/^[a-zA-Z0-9_\- ]\{$minLength,$m axLength}/";

        My understanding is that the backslash tells php to escape the
        following open brace. It seems to do that locally, but not on the
        shared server. Any reason why the behavior is different on the two
        systems?

        Comment

        • Michael Fesser

          #5
          Re: Windows/ Unix regular expression question

          ..oO(e_matthes@ hotmail.com)
          >I would still really like to know, however, why my local php
          >installation and the installation on my shared server interpret this
          >line differently:
          >$regExp = "/^[a-zA-Z0-9_\- ]\{$minLength,$m axLength}/";
          >
          >My understanding is that the backslash tells php to escape the
          >following open brace. It seems to do that locally, but not on the
          >shared server. Any reason why the behavior is different on the two
          >systems?
          Different PHP versions? From the manual:

          | Again, if you try to escape any other character, the backslash will be
          | printed too! Before PHP 5.1.1, backslash in \{$var} hasn't been
          | printed.

          and

          | Since you can't escape '{', [...]

          I would use sprintf() as suggested by Toby.

          Micha

          Comment

          • e_matthes@hotmail.com

            #6
            Re: Windows/ Unix regular expression question

            On May 30, 2:42 am, Michael Fesser <neti...@gmx.de wrote:
            .oO(e_matt...@h otmail.com)
            >
            I would still really like to know, however, why my local php
            installation and the installation on my shared server interpret this
            line differently:
            $regExp = "/^[a-zA-Z0-9_\- ]\{$minLength,$m axLength}/";
            >
            My understanding is that the backslash tells php to escape the
            following open brace. It seems to do that locally, but not on the
            shared server. Any reason why the behavior is different on the two
            systems?
            >
            Different PHP versions? From the manual:
            >
            | Again, if you try to escape any other character, the backslash will be
            | printed too! Before PHP 5.1.1, backslash in \{$var} hasn't been
            | printed.
            >
            and
            >
            | Since you can't escape '{', [...]
            >
            I would use sprintf() as suggested by Toby.
            >
            Micha

            Thank you, that's exactly it. It would have taken me a long time to
            figure that out.

            Comment

            Working...