473,614 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($mat ch, $res_descriptio n);
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 7442
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($mat ch, $res_descriptio n);
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($mat ch, $res_descriptio n);
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($mat ch, $res_descriptio n);
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($ma tch, $res))?('valid' ):('not valid'), "\n";

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

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

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

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

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

$res = "embedded\ttab" ;
echo 'Search: "', $res, '" is ', (preg_match($ma tch, $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($mat ch, $res_descriptio n);
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
4240
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 gifs). then it processes them based off of which image type it selected, either the st- 's or the sb- 's.
2
4690
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 = 3456789 preg_match ("|product=(\d{5,10})&amp;|i", $html, $out);
1
5395
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 characters Å Ä Ö. It should return false on strings containing stuff like -#¤%\"()=?&/. The thing is, I don't want to specifically allow Å Ä Ö, I want a more general rule in case other characters like ü or ø turn up. Is this possible to do? ...
5
8258
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 understand.)
4
5858
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 all work fine on the server. There's a little ASP that's to be put at the top of every page, which of course works fine for the .ASP pages. Now, if I want to include this same snippet of ASP code at the top of one of the PHP files, is that...
0
1336
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", I was not at all comforable with !preg_match() The manual is not clear about using ! with a non-boolean value. When using !($var) is ($var) converted to a boolean value, before
5
39957
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 preg_match function is recognised by PHP but it won't seem to accept any regexes as arguments. For example: $result = preg_match("","12")
4
14829
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 not interpreted in the preg_match ?
8
3991
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' ='~^.~' ); // make sure there is always a match
0
8142
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8642
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8591
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8444
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7115
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5549
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4058
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2575
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1438
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.