Jump to content

preg_match problem


jason360

Recommended Posts

Anyone know why my preg_match is being triggered by this term: 3D Hobby Shop

 

To me it should allow letters and numbers etc.

 

My php:

if(preg_match('/[^a-z0-9\-\_\,\!\?\+\ \.]+/i',$_POST['title']))
			{
				$err[]='<p class="error" style="color: #ed1c24;">Your Title contains invalid characters!</p>';
			}
Link to comment
https://forums.phpfreaks.com/topic/294952-preg_match-problem/
Share on other sites

UPDATE:  I realize it was this term triggering my preg_match:  42" AJ Slick

 

How can I get it to accept " ' and + 

 

 

This is what I am using now:

if(preg_match('/[^a-z0-9\-\_\,\!\?\+\"\'\ \.]+/i',$_POST['title']))
			{
				$err[]='<p class="error" style="color: #ed1c24;">Your Name contains invalid characters!</p>';
			}
Link to comment
https://forums.phpfreaks.com/topic/294952-preg_match-problem/#findComment-1507015
Share on other sites

That last one seems to be working for me. The if() condition is returning false as the string does not contain any characters outside the ones contained in the regular expression. Although that can be cleaned up a bit by using the \w wildcard which includes a-z, A-Z, 0-9 and _ (underscore).

<?php
 
$_POST['title'] = '42" AJ Slick';
 
if(preg_match('#[^\w\-\,\!\?\+\"\'\ \.]+#',$_POST['title']))
{
    echo "error";
    $err[]='<p class="error" style="color: #ed1c24;">Your Title contains invalid characters!</p>';
}
else
{
    echo "OK";
}
 
//Output: OK
 
?>
Link to comment
https://forums.phpfreaks.com/topic/294952-preg_match-problem/#findComment-1507016
Share on other sites

Thanks for the prompt reply Psycho.  I used your wildcard clean up and also found my problem.  When being posted I was getting \" instead of just \. 

 

Used stripslashes function.  Working like a charm now.

 

I couldn't figure it out...I felt like i was on crazy pills...haha

 

Thanks again!

 

JK

Link to comment
https://forums.phpfreaks.com/topic/294952-preg_match-problem/#findComment-1507021
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.