Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

return unmatched characters with a regex funtion, php

Posted on 2009-03-30
6
Medium Priority
?
743 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 1000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

963 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