jason360 Posted February 28, 2015 Share Posted February 28, 2015 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/294952-preg_match-problem/ Share on other sites More sharing options...
jason360 Posted February 28, 2015 Author Share Posted February 28, 2015 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/294952-preg_match-problem/#findComment-1507015 Share on other sites More sharing options...
Solution Psycho Posted February 28, 2015 Solution Share Posted February 28, 2015 (edited) 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 February 28, 2015 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/294952-preg_match-problem/#findComment-1507016 Share on other sites More sharing options...
jason360 Posted February 28, 2015 Author Share Posted February 28, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/294952-preg_match-problem/#findComment-1507021 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.