Solved

return unmatched characters with a regex funtion, php

Posted on 2009-03-30
6
696 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now