Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

preg_match and regex require tweaking

Status
Not open for further replies.

webdev007

Programmer
Sep 9, 2005
168
As part of a user input filter (coming from a text area)
I use the following regular expression
but it does not allow for text area line breaks <br> or <br />

I cannot figure how to include those line breaks in my regex
could you show me the way in such manner I will understand for futur references your addition.


if (!preg_match("/^[a-zA-Z0-9 \;\.\-\,\]+$/",$quest) )
 
Not sure I understand. Line-break in textarea is kind of "whitespace" (generalization of blankspace %20 to including \t,\r,\n). So why <br /> comes into the picture is beyond me. If you want to check if the textarea does not contain characters not allowed by the character class (but including "line break" etc, replace [tt][highlight] [/highlight][/tt] by "\s" should do.

>[tt]if (!preg_match("/^[a-zA-Z0-9[highlight] [/highlight]\;\.\-\,[highlight]\][/highlight]+$/",$quest) )[/tt]
[tt]if (!preg_match("/^[a-zA-Z0-9[red]\s[/red]\;\.\-\,[highlight]][/highlight]+$/",$quest) ) [/tt]

ps: There is this [tt]\][/tt] that I replace it by [tt]][/tt]: not sure whether it is a typo or it is your intention. If it is your intention to include "]" in the character class, then the pattern is ill-formed.
 
Thanks tsuji,
Yes the last " ] " was a typo

It still does not work
if the entered input is one line then it works fine
but any user generated "enter" or new line does trigger the filter.
I aggree with your statement regarding "Kind of white space"
but as is for some reason the new line can not be accepted?

I mentionned <br /> because out of curiosity
I went in phpMyAdmin and did a view source
it shows the breaks tags.
 
i'm not understanding your post. are you saying that you want a filter that will match anything that is not alphanumeric, certain punctuation and line breaks?

if so then would this not work?
Code:
$pattern = "/^[^\w\r\n\;\.\-\, ]+$/iD"; //allows underscores, alphanumerics line breaks, semicolon, comma, hyphen, space and full stop
if (preg_match($pattern, $filtertext)){
  //something wicked this way comes
}
 
jpadie, sorry but still does not work.
your statement is correct
<<<
will match anything that is not alphanumeric, certain punctuation and line breaks?
>>>
this is very strange what can be the root of the problem
knowing that there is nothing more simple that a teaxtarea, and my test is just made of simple words!
 
apologies. my logic was wrong as i am now using a positive preg_match
the pattern should be
Code:
$pattern = "/[^\w\s\;\.\-\,]/iD";
 
No needs for appologies, you helped me a great deal
this works perfectly!

PHP and TEXTAREA, seems to create quite some discussions
I searched extensively and even so php reported bugs (in my range of problems) are not acknowledged as real bugs, many coders are hitting a snag with those textarea new line breaks.
Again thanks all.
 
i note that you asked for an explanation of the script above. and that i did not provide one. so here you go

Code:
<?php
/**
 * [ create a character class.  ]
 * ^ negates the class. 
 * \w	any word character (alphanumeric and underscore)
 * \s	any whitespace character (carriage return, line feed, space and tab)
 * \; (etc) various punctuation marks
 * 
 * i	character insensitive (NOT NEEDED)
 * D	sets dollar to match at end of string only (not end of line prior to end of string). NOT NEEDED
 * 
 * so net effect is that if there is ANY character in the string that is NOT
 * one of the above then the pattern will match and you know there is an illegal
 * character
 * the modifiers i and D are not needed to make this work
 */
$pattern = "/[^\w\s\;\.\-\,]/iD";
if (preg_match($pattern, $text)){
	//illegal character found
}
?>
 
Thank you for your time and explanation
This programming area is so important but so "hermetic"
that learning it on your own is really a tough call!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top