You are here: Re: allowing single quote in preg_match « PHP Programming Language « IT news, forums, messages
Re: allowing single quote in preg_match

Posted by Pedro Graca on 10/11/06 14:17

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация