Link to home
Start Free TrialLog in
Avatar of joomla
joomlaFlag for Australia

asked on

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
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

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
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
ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joomla

ASKER

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 ????
Avatar of joomla

ASKER

Thanks for your help
You've highlighed other issues which will benefit me