Page 1 of 1

Str_replace - replacing a single quote with '

Posted: Fri Sep 30, 2005 1:17 am
by pilau
How should I replace a single quote, with the HTML entity &#39?

Posted: Fri Sep 30, 2005 1:25 am
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);

Posted: Fri Sep 30, 2005 3:10 am
by pilau
Thanks-a-lot, that solved my problem!

Posted: Fri Sep 30, 2005 3:18 am
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.

Posted: Fri Sep 30, 2005 3:23 am
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.

Posted: Fri Sep 30, 2005 3:29 am
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

Posted: Fri Sep 30, 2005 1:21 pm
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.

Posted: Fri Sep 30, 2005 4:34 pm
by Jenk
That's with magic quotes on, but if you used str_replace to remove the slashes, there would be none at all.

Posted: Fri Sep 30, 2005 4:42 pm
by feyd
Remove slashes: stripslashes()
Convert ' back to ' html_entity_decode()