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.
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.
Comment