Solved

return unmatched characters with a regex funtion, php

Posted on 2009-03-30
6
701 Views
Last Modified: 2013-12-12
Using PHP I need to validate a string in a function and eventually return something to the user like:

"Sorry, '<' is an invalid character"

I'm fine with just pulling the first unmatched character, but if it's easy enough we can show all of the invalid characters.

It's only for one small text field.

Allowed characters:
A-Z
a-z
0-9
!
?
'
"
-
(
)

preg_match('/^[A-Za-z0-9\s.\!\?\'\:\"\-\(\)]+$/',$value));

Open in new window

0
Comment
Question by:kenitech
  • 4
  • 2
6 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24025155
kenitech,

Try the expression in the snippet below.

Let me know if you have any questions or need more information.

b0lsc0tt
preg_match('/^[A-Za-z0-9 .!?':"()-]+$/',$value));

Open in new window

0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 250 total points
ID: 24025162
kenitech,

Oops.  I misread this a bit.  You can get all characters that don't match with ...

preg_match_all('/[^A-Za-z0-9.!?\':"()-]/', $value, $result, PREG_PATTERN_ORDER);
$result = $result[0];

The variable $result is an array.  You can change it to a string or go through each item.

Let me know if you have a question.

b0lsc0tt
0
 

Author Comment

by:kenitech
ID: 24025323
this is great thanks! before I accept/close, can you just please clarify the php documentation for preg_match_all?

http://us.php.net/preg_match_all

is it me or is it somewhat confusing that the $matches parameter is explained,
 
"Array of all matches in multi-dimensional array ordered according to flags ."

The output is the unmatched elements, though. This is what I want, but reading this leads me to believe otherwise.

thanks!
?
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24029432
Actually the output is the matched items.  If you notice I changed the expression so it will match the characters you don't want to allow.  Overlooking that change would make it seem to show non-matches.  Notice the ^ that is right after the open bracket ( [ ).
Let me know if you have any other questions on preg_match_all or what I provided.  I am glad it helped.
bol
0
 

Author Closing Comment

by:kenitech
ID: 31564648
Thanks!
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24029628
Your welcome!  I'm glad I could help.  Thanks for the grade, the points and the fun question.
bol
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question