joomla
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($News Title);
where $NewsTitle = "australia\'s day"
gives me the following: australia---'s-day
what I want is: australia's-day
can you help please
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\_\
where $NewsTitle = "australia\'s day"
gives me the following: australia---'s-day
what I want is: australia's-day
can you help please
What you seem to have is:
$NewsTitle = "australia\'s day";
print "Before: $NewsTitle\n";
$NewsTitle = preg_replace('/[^\w\d\-&\' ]/i','-',a ddslashes( strip_tags ($NewsTitl e)));
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','-',s trip_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
$NewsTitle = "australia\'s day";
print "Before: $NewsTitle\n";
$NewsTitle = preg_replace('/[^\w\d\-&\'
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\-&\'
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 ????
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 ????
ASKER
Thanks for your help
You've highlighed other issues which will benefit me
You've highlighed other issues which will benefit me
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)."
$NewsTitle = preg_replace('/[^\w\d\-&\'
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