Replace double quotes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mbowling
Forum Newbie
Posts: 4
Joined: Mon Aug 24, 2009 4:13 pm

Replace double quotes

Post 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.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Replace double quotes

Post by Darhazer »

Code: Select all

$title = 'something with "quotes"';
$title = str_replace('"', "'", $title);
echo $title;
exit;
Works fine for me.
mbowling
Forum Newbie
Posts: 4
Joined: Mon Aug 24, 2009 4:13 pm

Re: Replace double quotes

Post 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?
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Replace double quotes

Post by Darhazer »

What displays is not always what it contains:
var_dump( $row['ticket_title'] ); ?
mbowling
Forum Newbie
Posts: 4
Joined: Mon Aug 24, 2009 4:13 pm

Re: Replace double quotes

Post by mbowling »

string(30) "Testing "New" Ticket"
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Replace double quotes

Post by Darhazer »

I bet that it's actually

Code: Select all

Testing "New" Ticket
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 :)
mbowling
Forum Newbie
Posts: 4
Joined: Mon Aug 24, 2009 4:13 pm

Re: Replace double quotes

Post by mbowling »

You win the bet.

Testing "New" Ticket

I will modify str_replace to look for "

Thank you for taking time to help.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Replace double quotes

Post by Eran »

I will modify str_replace to look for "
don't do that. run html_entity_decode() before performing the substitution
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Replace double quotes

Post by jackpf »

Better yet, encode stuff when displaying it. The database should contain raw, unmodified data...or so I'm told ;)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Replace double quotes

Post 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
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Replace double quotes

Post 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 "...
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Replace double quotes

Post by Eran »

what I meant was, that sometimes some filtering / formatting is necessary (depending on the application requirements). escaping should be done regardless
Post Reply