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 9 21253
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/
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
*** 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
--
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/
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
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/
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
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/
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 thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: sankofa |
last post by:
hi, i can't seem to be able to escape my single quote properly... is it
even possible in javascript?
this is a portion of my code..
var DLEWIS="Pastor Lewis";
.... Sermon is a yser-defined...
|
by: Paul |
last post by:
I've got some code that adds a single quote to any ad hoc queries that
appear to look like hacks. For instance, if somebody enters ' OR 1=1 --
then this code adds another single quote the string...
|
by: Jack |
last post by:
Hi, I have a asp page where part of the code is as follows. This builds up
the sql statement partially.
sql01 = "UPDATE EquipmentTbl SET "
sql01 = sql01 & "SerialNumber = '" &...
|
by: acord |
last post by:
Hi,
I got this basic function not working properly when using differnet
browsers (IE6 and Firefox1.501). The problem is the single got striped
off from IE6,while FireFox preserved the single...
|
by: Anonieko |
last post by:
Hello ASPNET guru's,
What is a clean way to go around the problem of displaying a GridView
templated column where data can contain Single Quote ( ' )?
I maybe too naive, but this is of course...
|
by: Elmo |
last post by:
Hi all!
I am not very proud to ask this but here is my problem:
string code = "\'13\'"
The string code will have to contain following info:
'51','52','63','other'...
to get certain info...
|
by: Keeper |
last post by:
class object (COM-object) return dimension "System.Single".
problem:
Object o; // object = VARIANT
o = comclass.GetData();
Single dm = (Single) o; // Error, problem convertation
Help!!!
|
by: =?Utf-8?B?U2FpbXZw?= |
last post by:
Hello.
I have a problem about single quote search in C#.
I have 1 table:
ID NAME COMPANY
1 Sayre One
1' Sayre Two
|
by: Evan |
last post by:
Hello -
i'm trying to call subprocess.popen on the 'command-based' function in
linux. When I run the command from the shell, like so:
goset -f ' "%s %s" name addr ' file_name
it works fine...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |