|
Posted by e_matthes on 05/29/07 15:44
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($dirty, 0,50);
print "<br>dirty: $dirty<br>clean: $clean";
function getCleanText($textIn, $minLengthIn, $maxLengthIn) {
$acceptableTextRegExp = "/^[a-zA-Z0-9_\- ]\{$minLengthIn,
$maxLengthIn}/";
print "<br>$acceptableTextRegExp<br>";
if ( preg_match($acceptableTextRegExp, $textIn, $matchedArray) ) {
return $matchedArray[0];
} else {
return "";
}
} // END getCleanText($textIn, $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.
Navigation:
[Reply to this message]
|