Page 1 of 1
Replace double quotes
Posted: Mon Aug 24, 2009 4:19 pm
by mbowling
I need to replace double quotes with single quotes in a string.
I tried $title = str_replace('"', "'", $title);
First parameter is a double quote inclosed by single quotes.
Second parameter is a single quote inclosed by double quotes.
This doesn't work.
How do I replace double quotes with single quotes?
Thank you.
Re: Replace double quotes
Posted: Mon Aug 24, 2009 4:23 pm
by Darhazer
Code: Select all
$title = 'something with "quotes"';
$title = str_replace('"', "'", $title);
echo $title;
exit;
Works fine for me.
Re: Replace double quotes
Posted: Mon Aug 24, 2009 4:56 pm
by mbowling
You are correct. In the example you provided it does indeed work.
This is my situation where it does not work.
$ticket_title = $row['ticket_title'];
echo $ticket_title;
displays: Testing "New" Ticket
$ticket_title_edit = str_replace('"', "'", $ticket_title);
echo $ticket_title_edit;
displays: Testing "New" Ticket
Why are the double quotes not replaced?
Re: Replace double quotes
Posted: Mon Aug 24, 2009 4:59 pm
by Darhazer
What displays is not always what it contains:
var_dump( $row['ticket_title'] ); ?
Re: Replace double quotes
Posted: Mon Aug 24, 2009 5:03 pm
by mbowling
string(30) "Testing "New" Ticket"
Re: Replace double quotes
Posted: Mon Aug 24, 2009 5:06 pm
by Darhazer
I bet that it's actually
Otherwise it should be 20, not 30 characters long
View the html source in the browser and tell me if I'm winning the bet

Re: Replace double quotes
Posted: Mon Aug 24, 2009 5:14 pm
by mbowling
You win the bet.
Testing "New" Ticket
I will modify str_replace to look for "
Thank you for taking time to help.
Re: Replace double quotes
Posted: Mon Aug 24, 2009 5:19 pm
by Eran
I will modify str_replace to look for "
don't do that. run html_entity_decode() before performing the substitution
Re: Replace double quotes
Posted: Mon Aug 24, 2009 6:40 pm
by jackpf
Better yet, encode stuff when displaying it. The database should contain raw, unmodified data...or so I'm told

Re: Replace double quotes
Posted: Mon Aug 24, 2009 6:44 pm
by Eran
The database should contain raw, unmodified data...
That's dependent on the needs of the application. regardless, all user input should be escaped with a database specific functions
Re: Replace double quotes
Posted: Mon Aug 24, 2009 6:52 pm
by jackpf
Well, yeah.
But I'd never encode stuff inserting it into the database.
First of all, it's harder to get the original version back...which may be required for some reason.
Also, searches may not be correct. Someone actually searching for the term "&", looking for a post where someone has written & rather than an & sign which has been encoded would not work.
Also, encoded data takes up more space!! & compared to &, " compared to "...
Re: Replace double quotes
Posted: Mon Aug 24, 2009 7:08 pm
by Eran
what I meant was, that sometimes some filtering / formatting is necessary (depending on the application requirements). escaping should be done regardless