Jump to content

preg_match problem


Go to solution Solved by Psycho,

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

  • Solution

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
 
?>
Edited by Psycho
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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.