Results 1 to 4 of 4

Thread: Server / REGEX/ PHP Setting

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default Server / REGEX/ PHP Setting

    Is there a setting that would change the way this would be processed?
    PHP Code:
        $patterns = array();
        
    $replacements = array();
        
    $patterns[] = '/(\d) x 10(\d)/xi';
        
    $replacements[] = '$1 x 10<sup>$2</sup>';
        
    $contents preg_replace($patterns$replacements$input); 
    I haven't modified this code in months but it stopped working yesterday and I'm not sure why (this is a simplified version but all other replacements work as they should.
    Input: 5 x 105
    Expected Outcome and previous outcome: 5 x 10<sup>5</sup>
    Current Outcome: 5 x 105
    Corrections to my coding/thoughts welcome.

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Short answer: remove the /x modifier and your PCRE will work again.

    I never used the /x modifier, so I don't really know much about it.
    To choose the lesser of two evils is still to choose evil. My personal site

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    The first place to start here is to be sure that echoing the input and echoing the output is actually what the script seems to present. The most common server configuration issues change data such as when submitted through a form, not halfway through the execution. I don't see anything wrong with that, and if it worked before it should work now. Why don't you extract that code and test it by itself? If you've already done all of this, that's very strange. I don't see anything wrong, though...
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    The /x modifier will ignore whitespaces and allow for comments. I think php.net is awesome, but in the area of PCRE it is certainly lacking. The examples are sparse if present at all.

    You could do a php info to see which version of PCRE your server is running, but that is not your problem here. Older versions may have bugs, but from what I have seen the bugs would only be visible under some very unusual expressions.

    Anyway, lets look at several examples so as to give an idea about what PCRE is doing.

    /i means that it is case insensitive.
    /x means that you can use whitespace and comments in your PCRE. This is fine if you are going to have some very complicated PCRE and need to annotate your PCRE for later reference or for others to be able to read it.

    Starting with your PCRE
    Code:
    <?php
        $patterns = array(); 
        $replacements = array(); 
        $input = "5 x 105";
        $patterns[] = '/(\d) x 10(\d)/ix'; 
        $replacements[] = '$1 x 10<sup>$2</sup>'; 
        $contents = preg_replace($patterns, $replacements, $input); 
    echo "$contents";
    ?>
    Your script matches 5x105, not
    Code:
    5 x 105
    If you want it to match 5 x 105 you will need to use:
    Code:
    <?php
        $patterns = array(); 
        $replacements = array(); 
        $input = "5 x 104";
        $patterns[] = '/(\d)\s x \s10(\d)/ix'; 
        $replacements[] = '$1 x 10<sup>$2</sup>'; 
        $contents = preg_replace($patterns, $replacements, $input); 
    echo "$contents";
    ?>
    \s will match any whitespace. This can be a space or newline, carriage return, formfeed horizontal whitespace, or tab.

    When not using the /x modifier your PCRE should look like this:
    Code:
    <?php
        $patterns = array(); 
        $replacements = array(); 
        $input = "5 x 104";
        $patterns[] = '/(\d)\sx\s10(\d)/i'; 
        $replacements[] = '$1 x 10<sup>$2</sup>'; 
        $contents = preg_replace($patterns, $replacements, $input); 
    echo "$contents";
    ?>
    or
    Code:
    <?php
        $patterns = array(); 
        $replacements = array(); 
        $input = "5 x 104";
        $patterns[] = '/(\d) x 10(\d)/i'; 
        $replacements[] = '$1 x 10<sup>$2</sup>'; 
        $contents = preg_replace($patterns, $replacements, $input); 
    echo "$contents";
    ?>
    If you are only going to use one item in your array then you could make it simpler by using only variables as opposed to arrays, but if you plan on potentially using variable numbers of patterns and/or replacements then this is a good idea, but I have always found that to be needlessly complicated. Depends on what you need though.

    Code:
    <?php
        $input = "5 x 104";
        $patterns = '/(\d) x 10(\d)/i'; 
        $replacements= '$1 x 10<sup>$2</sup>'; 
        $contents = preg_replace($patterns, $replacements, $input); 
    echo "$contents";
    ?>
    or
    Code:
    <?php
        $input = "5 x 104";
        $contents = preg_replace('/(\d) x 10(\d)/i', '$1 x 10<sup>$2</sup>', $input); 
    echo "$contents";
    ?>
    is a little simpler, but again, it all depends on what your needs are.

    EDIT: I misspoke earlier. I have used the /x modifier before. I even wrote about it in one of my tutorials on PCRE .
    Last edited by james438; 04-27-2010 at 07:19 AM. Reason: fixed a typo.
    To choose the lesser of two evils is still to choose evil. My personal site

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •