473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

allowing single quote in preg_match

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
5 7393
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: fartsniff | last post by:
hello all, here is a preg_match routine that i am using. basically, $image is set in some code above, and it can be either st-1.gif or sb-1.gif (actually it randomly picks them from about 100...
2
by: Han | last post by:
I'm wondering if someone can explain why the following works with preg_match_all, but not preg_match: $html = "product=3456789&amp;" preg_match_all ("|product=(\d{5,10})&amp;|i", $html, $out); $out...
1
by: Daniel | last post by:
I've been searching the net and Google news groups for a preg_match expression that will return true on strings containing (uppercase and lowercase) characters of A-Z, 0-9 and for instance Swedish...
5
by: sinister | last post by:
The examples in the online manual all seem to use double quotes, e.g. at http://us3.php.net/preg_replace Why? (The behavior is different with single quotes, and presumably simpler to...
4
by: Dan S | last post by:
I'm a software engineer but pretty naive about web programming/scripting. I'm working with a server running Windows IIS. I have several ASP files and several PHP files (not developed by me) that...
0
by: awebguynow | last post by:
I ran across this code, and it kind of made me nervous: (as an email validator) if ( !preg_match("/.*\@.*\..*/", $_POST) | preg_match("/(\)/", $_POST) ) 1) from bitwise experience with "C",...
5
by: Andrew Richardson | last post by:
Apologies if this has been asked before - I can't find anything on Google or Google Groups. I am running PHP 5.0.4 on Apache 2.0.54 with the PCRE extension installed. For some reason though, the...
4
by: squash | last post by:
I have a string equal to 'www/' that I want to use in a preg_match. Php keeps giving me the warning: Warning: preg_match(): Unknown modifier '/' How can I escape the string so the / in www/ is...
8
by: Thomas Mlynarczyk | last post by:
Hello, I want to split a given string into tokens which are defined by regexes: // example tokens - a bit more complex in real $tokens = array( 'NUMBER' ='~^\d+~', 'NAME' ='~^+~', 'ANY' ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.