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

allowing single quote in preg_match

P: n/a
Hi all,

I'm trying to validate text in a HTML input field.
How do I *allow* a single quote?

// catch any nasty characters (eg !@#$%^&*()/\)
$match = '/^['\w\.\-_?!, ]+$/';
$valid_srch = preg_match($match, $res_description);
if (!$valid_srch) {
...
}

for $match I've also tried:

$match = '/^[\'\w\.\-_?!, ]+$/';

$match = '/^["\w\.\-_?!, ]+$/';

$match = "/^['\w\.\-_?!, ]+$/";

$match = "/^[\'\w\.\-_?!, ]+$/";

and numerous other strings but always ' is seen as an
invalid character.
thanks,
--
Mark
Oct 11 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Try backslashing the single quote: $match = '/^[\'\w\.\-_?!, ]+$/';

In a single-quoted string, a literal single quote must be escaped with
a backslash. See here for string usage:

http://us2.php.net/manual/en/language.types.string.php

Mark Woodward wrote:
Hi all,

I'm trying to validate text in a HTML input field.
How do I *allow* a single quote?

// catch any nasty characters (eg !@#$%^&*()/\)
$match = '/^['\w\.\-_?!, ]+$/';
$valid_srch = preg_match($match, $res_description);
if (!$valid_srch) {
...
}

for $match I've also tried:

$match = '/^[\'\w\.\-_?!, ]+$/';

$match = '/^["\w\.\-_?!, ]+$/';

$match = "/^['\w\.\-_?!, ]+$/";

$match = "/^[\'\w\.\-_?!, ]+$/";

and numerous other strings but always ' is seen as an
invalid character.
thanks,
--
Mark
Oct 11 '06 #2

P: n/a
Hi petersprc,

On Wed, 11 Oct 2006 05:55:43 -0700, petersprc wrote:
Try backslashing the single quote: $match = '/^[\'\w\.\-_?!, ]+$/';
I've tried that but still seen as an illegal character??? Weird!
(See first alternative match below).

Could it be an encoding issue?

>
In a single-quoted string, a literal single quote must be escaped with
a backslash. See here for string usage:

http://us2.php.net/manual/en/language.types.string.php

Mark Woodward wrote:
>Hi all,

I'm trying to validate text in a HTML input field.
How do I *allow* a single quote?

// catch any nasty characters (eg !@#$%^&*()/\)
$match = '/^['\w\.\-_?!, ]+$/';
$valid_srch = preg_match($match, $res_description);
if (!$valid_srch) {
...
}

for $match I've also tried:

$match = '/^[\'\w\.\-_?!, ]+$/';

$match = '/^["\w\.\-_?!, ]+$/';

$match = "/^['\w\.\-_?!, ]+$/";

$match = "/^[\'\w\.\-_?!, ]+$/";

and numerous other strings but always ' is seen as an
invalid character.
thanks,
--
Mark

cheers,

--
Mark

Oct 11 '06 #3

P: n/a
It could be an encoding issue. If your input string is utf8, try adding
the /u modifier to the pattern.

You could also try error_reporting(E_ALL) as a precaution. Could you
paste your whole function and the var_dump output of your input string?

Mark Woodward wrote:
Hi petersprc,

On Wed, 11 Oct 2006 05:55:43 -0700, petersprc wrote:
Try backslashing the single quote: $match = '/^[\'\w\.\-_?!, ]+$/';

I've tried that but still seen as an illegal character??? Weird!
(See first alternative match below).

Could it be an encoding issue?


In a single-quoted string, a literal single quote must be escaped with
a backslash. See here for string usage:

http://us2.php.net/manual/en/language.types.string.php

Mark Woodward wrote:
Hi all,

I'm trying to validate text in a HTML input field.
How do I *allow* a single quote?

// catch any nasty characters (eg !@#$%^&*()/\)
$match = '/^['\w\.\-_?!, ]+$/';
$valid_srch = preg_match($match, $res_description);
if (!$valid_srch) {
...
}

for $match I've also tried:

$match = '/^[\'\w\.\-_?!, ]+$/';

$match = '/^["\w\.\-_?!, ]+$/';

$match = "/^['\w\.\-_?!, ]+$/";

$match = "/^[\'\w\.\-_?!, ]+$/";

and numerous other strings but always ' is seen as an
invalid character.
thanks,
--
Mark


cheers,

--
Mark
Oct 11 '06 #4

P: n/a
Mark Woodward wrote:
I'm trying to validate text in a HTML input field.
How do I *allow* a single quote?
The following code works for me
<?php
// catch any nasty characters (eg !@#$%^&*()/\)
$match = '/^[\'\w\._?!, -]+$/';

$res = 'word';
echo 'Search: "', $res, '" is ', (preg_match($match, $res))?('valid'):('not valid'), "\n";

$res = 'two words';
echo 'Search: "', $res, '" is ', (preg_match($match, $res))?('valid'):('not valid'), "\n";

$res = 'pseudo-word';
echo 'Search: "', $res, '" is ', (preg_match($match, $res))?('valid'):('not valid'), "\n";

$res = 'Mc\'Donalds';
echo 'Search: "', $res, '" is ', (preg_match($match, $res))?('valid'):('not valid'), "\n";

$res = 'under_score';
echo 'Search: "', $res, '" is ', (preg_match($match, $res))?('valid'):('not valid'), "\n";

$res = 'Question? Answer!';
echo 'Search: "', $res, '" is ', (preg_match($match, $res))?('valid'):('not valid'), "\n";

$res = "embedded\ttab";
echo 'Search: "', $res, '" is ', (preg_match($match, $res))?('valid'):('not valid'), "\n";
?>
// catch any nasty characters (eg !@#$%^&*()/\)
$match = '/^['\w\.\-_?!, ]+$/';
// * 1 22

*1 the single quote here is invalid; you need to escape it
*2 I'm not sure an escaped "-" works. What I do when I want a "-" in
a regular expression inside a class definition is to put it at the
end

--
File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot
Oct 11 '06 #5

P: n/a
On Wed, 11 Oct 2006 22:40:05 +1000, Mark Woodward wrote:
Hi all,

I'm trying to validate text in a HTML input field.
How do I *allow* a single quote?

// catch any nasty characters (eg !@#$%^&*()/\)
$match = '/^['\w\.\-_?!, ]+$/';
$valid_srch = preg_match($match, $res_description);
if (!$valid_srch) {
...
}

for $match I've also tried:

$match = '/^[\'\w\.\-_?!, ]+$/';

$match = '/^[&quot;\w\.\-_?!, ]+$/';

$match = "/^['\w\.\-_?!, ]+$/";

$match = "/^[\'\w\.\-_?!, ]+$/";

and numerous other strings but always ' is seen as an
invalid character.
thanks,
Peter/ Pedro,

sorry I haven't responded. All good now. I decided to attack it from a
different angle (put it on the TODO pile ;-)).

thanks,

--
Mark
Oct 12 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.