By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,569 Members | 1,394 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,569 IT Pros & Developers. It's quick & easy.

problem with str_replace and single quote

P: n/a
I need to use the str_replace function for a php-script which works
together with html/javascript.
In the script I have to perform things like:
$arabic=str_replace ("'","\'", $row[0]);
$arabic=str_replace ("sh", "\š", $row[0]);

the second line works perfectly, however I can't make the first one
work, it should replace a single quote with a quote preceded by a
backslash. I presume I have to escape some characters to make this
happen however I can't figure out how.
any help welcome
best regards,
Hugo

b.t.w. I can't use the htmlspecialchars-function as this will interfere
with other code in the script

Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Hugo Coolens wrote:
I need to use the str_replace function for a php-script which works
together with html/javascript.
In the script I have to perform things like:
$arabic=str_replace ("'","\'", $row[0]);
$arabic=str_replace ("sh", "\š", $row[0]);

the second line works perfectly, however I can't make the first one
work, it should replace a single quote with a quote preceded by a
backslash. I presume I have to escape some characters to make this
happen however I can't figure out how.
any help welcome
best regards,
Hugo

b.t.w. I can't use the htmlspecialchars-function as this will interfere
with other code in the script


You need to escape the backslash with another backslash, otherwise you're
escaping the single quote.

$arabic=str_replace ("'","\\'", $row[0]);

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #2

P: n/a
Chris Hope wrote:
Hugo Coolens wrote:

I need to use the str_replace function for a php-script which works
together with html/javascript.
In the script I have to perform things like:
$arabic=str_replace ("'","\'", $row[0]);
$arabic=str_replace ("sh", "\š", $row[0]);

the second line works perfectly, however I can't make the first one
work, it should replace a single quote with a quote preceded by a
backslash. I presume I have to escape some characters to make this
happen however I can't figure out how.
any help welcome
best regards,
Hugo

b.t.w. I can't use the htmlspecialchars-function as this will interfere
with other code in the script

You need to escape the backslash with another backslash, otherwise you're
escaping the single quote.

$arabic=str_replace ("'","\\'", $row[0]);

Unfortunately this does not work, I already tried that, the problem
seems to be the first single quote in the expression.

regards,
hugo

Jul 17 '05 #3

P: n/a
*** Hugo Coolens wrote/escribió (Thu, 16 Sep 2004 09:51:28 +0200):
$arabic=str_replace ("'","\\'", $row[0]);

Unfortunately this does not work, I already tried that, the problem
seems to be the first single quote in the expression.


This works fine for me (I've just tested it). Are you sure there are
actually single quote chars in your text and not only tildes like ` or ´?
--
-- Álvaro G. Vicario - Burgos, Spain
-- Thank you for not e-mailing me your questions
--
Jul 17 '05 #4

P: n/a
Hugo Coolens wrote:
Chris Hope wrote:
Hugo Coolens wrote:

I need to use the str_replace function for a php-script which works
together with html/javascript.
In the script I have to perform things like:
$arabic=str_replace ("'","\'", $row[0]);
$arabic=str_replace ("sh", "\š", $row[0]);

the second line works perfectly, however I can't make the first one
work, it should replace a single quote with a quote preceded by a
backslash. I presume I have to escape some characters to make this
happen however I can't figure out how.
any help welcome
best regards,
Hugo

b.t.w. I can't use the htmlspecialchars-function as this will interfere
with other code in the script

You need to escape the backslash with another backslash, otherwise you're
escaping the single quote.

$arabic=str_replace ("'","\\'", $row[0]);

Unfortunately this does not work, I already tried that, the problem
seems to be the first single quote in the expression.


Worked when I tested it. What's the result you're getting?

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #5

P: n/a
Chris Hope wrote:
Hugo Coolens wrote:
[ ... ]
$arabic=str_replace ("'","\'", $row[0]);


[ ... ]
You need to escape the backslash with another backslash, otherwise you're
escaping the single quote.

$arabic=str_replace ("'","\\'", $row[0]);


No. '\\'' in a double-quoted string is equivalent to '\''.
In the latter the backslash is taken literally, because '\''
isn't an escape sequence; in the former the first backslash
escapes the second, leaving a backslash and apostrophe.

http://www.php.net/manual/en/language.types.string.php

--
Jock
Jul 17 '05 #6

P: n/a
John Dunlop wrote:
Chris Hope wrote:
Hugo Coolens wrote:


[ ... ]
> $arabic=str_replace ("'","\'", $row[0]);


[ ... ]
You need to escape the backslash with another backslash, otherwise you're
escaping the single quote.

$arabic=str_replace ("'","\\'", $row[0]);


No. '\\'' in a double-quoted string is equivalent to '\''.
In the latter the backslash is taken literally, because '\''
isn't an escape sequence; in the former the first backslash
escapes the second, leaving a backslash and apostrophe.

http://www.php.net/manual/en/language.types.string.php


That's what he wanted, and I quote: "it should replace a single quote with a
quote preceded by a backslash". So by escaping the backslash he ends up
with a backslash followed by a single quote.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #7

P: n/a
Chris Hope wrote:
That's what he wanted, and I quote: "it should replace a single quote with a
quote preceded by a backslash". So by escaping the backslash he ends up
with a backslash followed by a single quote.


And not escaping the backslash leaves him with a backslash
followed by an apostrophe too. ;o)

I was pointing out that there's no need to escape the
backslash, and that H. Coolens' assignment was equivalent to
yours. My 'no' was disagreeing with your 'you need to
escape the backslash with another backslash, otherwise
you're escaping the single quote', because, in a double-
quoted string, '\'' means a backslash followed by an
apostrophe. No escaping occurs.

Sorry if I wasn't clear.

--
Jock
Jul 17 '05 #8

P: n/a
John Dunlop wrote:
Chris Hope wrote:
That's what he wanted, and I quote: "it should replace a single quote
with a quote preceded by a backslash". So by escaping the backslash he
ends up with a backslash followed by a single quote.


And not escaping the backslash leaves him with a backslash
followed by an apostrophe too. ;o)

I was pointing out that there's no need to escape the
backslash, and that H. Coolens' assignment was equivalent to
yours. My 'no' was disagreeing with your 'you need to
escape the backslash with another backslash, otherwise
you're escaping the single quote', because, in a double-
quoted string, '\'' means a backslash followed by an
apostrophe. No escaping occurs.

Sorry if I wasn't clear.


You're right for double quoted strings, which was in his example originally,
so I was wrong about that :) I think I was just assuming that was the
answer without checking "\'" myself. Funny that it wasn't working for him
in the first place then.

print "\'"; // this will output \'

But if you're using single quotes, as in your post, then you just get a
single quote:

print '\''; // this will output '

Cheers,
Chris

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #9

P: n/a
Chris Hope wrote:
Hugo Coolens wrote:

Chris Hope wrote:
Hugo Coolens wrote:

I need to use the str_replace function for a php-script which works
together with html/javascript.
In the script I have to perform things like:
$arabic=str_replace ("'","\'", $row[0]);
$arabic=str_replace ("sh", "\š", $row[0]);

the second line works perfectly, however I can't make the first one
work, it should replace a single quote with a quote preceded by a
backslash. I presume I have to escape some characters to make this
happen however I can't figure out how.
any help welcome
best regards,
Hugo

b.t.w. I can't use the htmlspecialchars-function as this will interfere
with other code in the script
You need to escape the backslash with another backslash, otherwise you're
escaping the single quote.

$arabic=str_replace ("'","\\'", $row[0]);


Unfortunately this does not work, I already tried that, the problem
seems to be the first single quote in the expression.

Worked when I tested it. What's the result you're getting?

I discovered my mistake, I just wast overwriting my variable again and
again, therefore I didn't get the expected result.
Thank you all for the comments on escaping and mea culpa, mea culpa mea
maxima culpa

hugo

Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.