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 | |
Share this Question
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/ | |
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 | |
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
-- | |
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/ | |
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 | |
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/ | |
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 | |
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/ | |
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 | | This discussion thread is closed Replies have been disabled for this discussion. | | Question stats - viewed: 19315
- replies: 9
- date asked: Jul 17 '05
|