View Full Version : Server / REGEX/ PHP Setting
bluewalrus
04-21-2010, 04:54 PM
Is there a setting that would change the way this would be processed?
$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
james438
04-22-2010, 07:13 AM
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.
djr33
04-22-2010, 09:50 PM
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...
james438
04-22-2010, 11:22 PM
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
<?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
5 x 105 If you want it to match 5 x 105 you will need to use:
<?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:
<?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
<?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.
<?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
<?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 ;).
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.