Solved

PHP Regular Expression and Apostrophe!

Posted on 2006-06-12
16
1,376 Views
Last Modified: 2013-11-30
For the life of me I cannot get apostrophe usage down with Perl Regular Expressions.  Here is my regex:

if(preg_match('/^[a-zA-Z\'\.-]{2,30}$/',$input)){
      echo 'Hooray!';
}

My goal is for it to accept 2 - 30 alpha characters and periods, hyphens and apostrophes.  Everything works except apostrophes.  

0
Comment
Question by:mynamebecory2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
16 Comments
 

Expert Comment

by:krite
ID: 16886655
Try

if(preg_match('/^[a-zA-Z/\'/\.-]{2,30}$/',$input)){
     echo 'Hooray!';
}
0
 
LVL 1

Author Comment

by:mynamebecory2
ID: 16887031
It throws an error saying that ''' is an invalid modifier.  I am using PHP v5
0
 
LVL 1

Author Comment

by:mynamebecory2
ID: 16887055
With my original code it simply denies it as if it were improper input.  
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 8

Expert Comment

by:Autogard
ID: 16887102
Can you just put your regular expression pattern in double quotes instead of single?

if(preg_match("/^[a-zA-Z\'\.-]{2,30}$/",$input)){
     echo 'Hooray!';
}
0
 
LVL 8

Expert Comment

by:Autogard
ID: 16887140
I believe you could also, if you want to keep single-quoting in your coding style do something like this (yes, that's three backslashes before the single quote), but I'd recommend my first suggestion (above)...

if(preg_match('/^[a-zA-Z\\\'\.-]{2,30}$/',$input)){
     echo 'Hooray!';
}
0
 
LVL 1

Author Comment

by:mynamebecory2
ID: 16887192
Still denies it... Here is my exact code:


function validateName($input){
      #Cannot get apostrophe to work.
      $errorMsg = 'May only consist of (a-z, A-Z, \', ., -, and space)';
      if($input == ''){
            $errorMsg = 'This item is required.';
      } else if(preg_match("/^[a-zA-Z\'\.-]{2,30}$/",$input)){
            $errorMsg = '';
      }
      return $errorMsg;
}

I tried it the second way with the single quotes and 3 slashes and it denied as well.
0
 
LVL 8

Expert Comment

by:Autogard
ID: 16887214
Can you give us the $input you are passing in?
0
 
LVL 8

Expert Comment

by:Autogard
ID: 16887240
By the way... it says that you are allowing spaces, but the regular expression pattern doesn't have a space in it (I don't know if experts-exchange is just trimming out that space for some reason).  So if your input has a space in it it will fail the regular expression.

This should have a space in it (before the closing "]"):
"/^[a-zA-Z\'\.- ]{2,30}$/"

Maybe it needs to be quoted:
"/^[a-zA-Z\'\.-\ ]{2,30}$/"

Sorry, just testing it -- I've seen spaces get trimmed out before.
0
 
LVL 1

Author Comment

by:mynamebecory2
ID: 16887362
I took the space out, I thought it was causing the problem.  The code above is exact, EE isn't stripping anything.

The input is these three names.  The function is called from a loop.

O'Dell
O'Brian
O'Brian-Mc.Donald (What an unfortunate married name eh?)
0
 
LVL 8

Accepted Solution

by:
Autogard earned 55 total points
ID: 16887429
This exact code works just fine on my system:

<?php

function validateName($input)
{
  $errorMsg = 'May only consist of (a-z, A-Z, \', ., -, and space)';

  if(preg_match("/^[a-zA-Z\'\.-]{2,30}$/",$input))
  {
       $errorMsg = '';
  }

  return $errorMsg;
}

$namesArray = array("O'Dell", "O'Brian", "O'Brian-Mc.Donald");

foreach($namesArray as $input)
{
  print "Input: $input, Message: \"" . validateName($input) . "\".<br>";
}

?>

Output:
Input: O'Dell, Message: "".
Input: O'Brian, Message: "".
Input: O'Brian-Mc.Donald, Message: "".

...what are you getting back from your call to the function?  If you are getting back the empty string it means that your regular expression passed (because you are setting errorMsg to '').
0
 
LVL 1

Author Comment

by:mynamebecory2
ID: 16887809
I think I found the problem...  I don't know what it is, but I found it :)

My loop isn't in the same file.  Its submitting it through a form...  So its actually coming in as post data.  If I take the regexp and put it in a new .php file and run it it works fine.  When I do it through the form it fails.  Magic quotes are turned off and I am not adding slashes (at this time).
0
 
LVL 8

Expert Comment

by:Autogard
ID: 16888116
Glad you fixed it!  So if I understand correctly you are passing those last names (with quotes, etc...) through a form and then checking them against a regular expression?

To further help with this I would try printing out each name before you check the regular expression to see how the form is handling those special characters.
0
 
LVL 1

Author Comment

by:mynamebecory2
ID: 16888206
Well, I didn't fix it...  I found the problem :)
Your regex works great, but it fails when passed through a form!?!
0
 
LVL 8

Expert Comment

by:Autogard
ID: 16888258
Yes, so you are passing "O'Brian-Mc.Donald" through a form and reading it like $_POST["lastname"].

Before you check the regular expression try printing out the value and let us know what it is (so that we can see what is happening to the form value when it comes through).  Then we'll be able to help you fix it.
0
 
LVL 1

Author Comment

by:mynamebecory2
ID: 16893741
Web Host had magic quotes on after all.  All fixed! :)
0
 
LVL 8

Expert Comment

by:Autogard
ID: 16893967
Great!  Yeah it sure looked like you were getting some garbled post data.  Glad things are working!
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Node.js 11 76
QoS on Cisco router 10 57
hsrp tracking 2 58
Move WSUS to Server 2016 3 35
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

737 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