Replace double quotes
Moderator: General Moderators
Replace double quotes
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.
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
Code: Select all
$title = 'something with "quotes"';
$title = str_replace('"', "'", $title);
echo $title;
exit;Re: Replace double quotes
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?
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
What displays is not always what it contains:
var_dump( $row['ticket_title'] ); ?
var_dump( $row['ticket_title'] ); ?
Re: Replace double quotes
string(30) "Testing "New" Ticket"
Re: Replace double quotes
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
Code: Select all
Testing "New" TicketView the html source in the browser and tell me if I'm winning the bet
Re: Replace double quotes
You win the bet.
Testing "New" Ticket
I will modify str_replace to look for "
Thank you for taking time to help.
Testing "New" Ticket
I will modify str_replace to look for "
Thank you for taking time to help.
Re: Replace double quotes
don't do that. run html_entity_decode() before performing the substitutionI will modify str_replace to look for "
Re: Replace double quotes
Better yet, encode stuff when displaying it. The database should contain raw, unmodified data...or so I'm told 
Re: Replace double quotes
That's dependent on the needs of the application. regardless, all user input should be escaped with a database specific functionsThe database should contain raw, unmodified data...
Re: Replace double quotes
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 "...
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
what I meant was, that sometimes some filtering / formatting is necessary (depending on the application requirements). escaping should be done regardless