Str_replace - replacing a single quote with '

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
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Str_replace - replacing a single quote with '

Post by pilau »

How should I replace a single quote, with the HTML entity &#39?
ruchit
Forum Commoner
Posts: 53
Joined: Mon Sep 26, 2005 6:03 am

Post by ruchit »

Code: Select all

str_replace("'", "#39;", $subject);
PS: If you're looking at this for backslshing problem, then use

Code: Select all

str_replace("\", "", $subject);
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Thanks-a-lot, that solved my problem!
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

ruchit wrote: PS: If you're looking at this for backslshing problem, then use

Code: Select all

str_replace("", "", $subject);
This is incorrect, well, syntactically it is correct, but it is inappropraite for this use. :)

That would remove all backslashes, not just the ones inputted by magic_quotes.

Use stripslashes to remove the escaping back slashes.
ruchit
Forum Commoner
Posts: 53
Joined: Mon Sep 26, 2005 6:03 am

Post by ruchit »

:oops: i am so wrong... but jenk did u read the other thread?? ... and if you didn't... pilau only asked for str_replace function on this thread.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I'v also just noticed it is syntactically incorrect aswell :P

it should be

Code: Select all

<?php

$string = str_replace ("\\", "", $string);

?>
But the problem with using the above, is what happens when a user wants to post a backslash?

For example, the below input
FAO: Bob - You can find the shared directory at: \\server\folder
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

If a user inputs the following:
"Doug's horse jumped the fence," said \\stable\pen.
would translate to this:
\"Doug\'s horse jumped the fence,\" said \\\\stable\\pen.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

That's with magic quotes on, but if you used str_replace to remove the slashes, there would be none at all.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Remove slashes: stripslashes()
Convert ' back to ' html_entity_decode()
Post Reply