?
Solved

return unmatched characters with a regex funtion, php

Posted on 2009-03-30
6
Medium Priority
?
733 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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

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!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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.
Suggested Courses

719 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