Solved

return unmatched characters with a regex funtion, php

Posted on 2009-03-30
6
706 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 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
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
if (is_singular not working 5 39
$_GET call between URL 3 38
php subtract from date 2 27
Increase counter and attr inside a while loop 15 34
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

756 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