?
Solved

Regular Expressions and apostraphes in PHP

Posted on 2011-10-16
5
Medium Priority
?
207 Views
Last Modified: 2012-06-22
I have an input field in a html form with PHP scripting
If someone enters the words "australia's day"
I store "australia\'s day" in my SQL table

I use the following script to substitute spaces with a dash

preg_replace('/[^a-z0-9\_\-\&\']/i','-',strip_tags($NewsTitle);
where $NewsTitle = "australia\'s day"

gives me the following:      australia---'s-day

what I want is:   australia's-day

can you help please
0
Comment
Question by:joomla
  • 3
  • 2
5 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36977027
You should be storing "australia's day" in your SQL table and the code that displays the data should add the \

Secondly, there must be something going on outside of the code you describe. This code:
$NewsTitle = "australia\'s day";
print "Before: $NewsTitle\n";
print "Tags stripped: ".strip_tags($NewsTitle)."\n";
$NewsTitle = preg_replace('/[^\w\d\-&\']/i','-',strip_tags($NewsTitle));
print "After: $NewsTitle\n";

Gives this result:
Before: australia\'s day
Tags stripped: australia\'s day
After: australia-'s-day

ie not the result you describe. Are you using addslashes somewhere? That would explain it I think
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36977052
What you seem to have is:

$NewsTitle = "australia\'s day";
print "Before: $NewsTitle\n";
$NewsTitle = preg_replace('/[^\w\d\-&\']/i','-',addslashes(strip_tags($NewsTitle)));
print "After: $NewsTitle\n";

Output:

Before: australia\'s day
After: australia---'s-day

What you should have is:

$NewsTitle = "australia's day";
print "Data in db: $NewsTitle\n";
$NewsTitle = preg_replace('/[^\w\d\-&\']/i','-',strip_tags($NewsTitle));
print "Data tidied up: $NewsTitle\n";
$NewsTitle = addslashes($NewsTitle);
print "Output the data like this: $NewsTitle\n";

Output:

Data in db: australia's day
Data tidied up: australia's-day
Output the data like this: australia\'s-day
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 36977062
Continuing my last post, the end result is that user should see "australia's-day" which is what you want.

If you can't fix up the data in your database, then this might work for you (have added stripslashes):

$NewsTitle = "australia\'s day";
print "Data in db: $NewsTitle\n";
$NewsTitle = preg_replace('/[^\w\d\-&\']/i','-',strip_tags(stripslashes($NewsTitle)));
print "Data tidied up: $NewsTitle\n";
$NewsTitle = addslashes($NewsTitle);
print "Output the data like this: $NewsTitle\n";

Output:

Data in db: australia\'s day
Data tidied up: australia's-day
Output the data like this: australia\'s-day
0
 

Author Comment

by:joomla
ID: 36977092
Hi TerryAtOpus,
When storing text to the database I use the following routine

function safeSQL($expr)
{
      if( is_bool( $expr ) )
      {
            if( $expr )
                  return '1';
            else
                  return '0';
      }
      elseif( is_double($expr) or is_float($expr) )
      {
            return strval(doubleval($expr));
      }
      elseif( is_int($expr) or is_long($expr) )
      {
            return strval(intval($expr));
      }
      elseif( is_null($expr) )
      {
            return '\'\'';
      }
      else
      {
      return mysql_real_escape_string( $expr );
      }
}

So, the input "australa's day" is stored as "australia\'s day"

I then use the following routine to display the field

function fromSQL($value)
{
    $value = is_array($value) ? array_map('fromSQL', $value) : stripslashes($value);
    return $value;
}


Is this not adviseable ????
0
 

Author Closing Comment

by:joomla
ID: 36977239
Thanks for your help
You've highlighed other issues which will benefit me
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month16 days, 1 hour left to enroll

850 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