Regular Expressions and apostraphes in PHP

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
joomlaAsked:
Who is Participating?
 
Terry WoodsConnect With a Mentor IT GuruCommented:
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
 
Terry WoodsIT GuruCommented:
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
 
Terry WoodsIT GuruCommented:
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
 
joomlaAuthor Commented:
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
 
joomlaAuthor Commented:
Thanks for your help
You've highlighed other issues which will benefit me
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.