By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,471 Members | 722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,471 IT Pros & Developers. It's quick & easy.

Windows/ Unix regular expression question

P: n/a
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.

May 29 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
e_matthes wrote:
$acceptableTextRegExp = "/^[a-zA-Z0-9_\- ]\{$minLengthIn, $maxLengthIn}/";
$acceptableTextRegExp = sprintf('/^[a-zA-Z0-9 _-]{%d,%d}$/',
(int)$minLengthIn,
(int)$maxLengthIn);
--
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
http://tobyinkster.co.uk/blog/2007/0...tive-surnames/
May 29 '07 #2

P: n/a
e_*******@hotmail.com wrote:
$acceptableTextRegExp = "/^[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:

$acceptableTextRegExp = '/^[a-zA-Z0-9_\-
]{'.$minLengthIn.'.'.$maxLengthIn.'}/';
May 29 '07 #3

P: n/a
On May 29, 3:32 pm, Alexey Kulentsov <criman...@crimaniak.comwrote:
e_matt...@hotmail.com wrote:
$acceptableTextRegExp = "/^[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:

$acceptableTextRegExp = '/^[a-zA-Z0-9_\-
]{'.$minLengthIn.'.'.$maxLengthIn.'}/';

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

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,$maxLength}/";

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?

May 30 '07 #4

P: n/a
..oO(e_*******@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,$maxLength}/";

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
May 30 '07 #5

P: n/a
On May 30, 2:42 am, Michael Fesser <neti...@gmx.dewrote:
.oO(e_matt...@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,$maxLength}/";
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.

May 30 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.