473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

problem with str_replace and single quote

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
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/
Jul 17 '05 #2
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
*** 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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
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...
4
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...
4
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 = '" &...
4
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...
2
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...
11
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...
4
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!!!
1
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
3
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.