472,958 Members | 1,643 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Replacing double quotes with single quotes

Hi all,

I'm looking for a way to scan a block of text and replace all the
double quotes (") with single quotes (').

I'm using PHP to pull text out of a mySQL table and then feed the text
into a javascript function called by onClick. The problem is that the
text may contain single or double quotes, which screws up the
javascript. I eliminated the single quote problem by running the text
through addslashes, but the double quotes are a problem.
Here is an example.

mySQL text:
the "father's" absorption, preoccupation and interest in the infant

after addslashes:
the \"father\'s\" absorption, preoccupation and interest in the infant

attempting to use in onClick:
onclick="printAnswer('the \"father\'s\ absorption, preoccupation and
interest in the infant')

Error in the javascript console:
Error: unterminated string literal
Source Code:
printAnswer('the \

Any tips or suggestions would be very welcome!

Jul 17 '05 #1
11 35809

Jakanapes wrote:
Hi all,

I'm looking for a way to scan a block of text and replace all the
double quotes (") with single quotes (').


Well, you could use str_replace('"',"'",$text) or use
htmlentities($text,ENT_QUOTES).

Example:
<?
$text = "This is a string with 'single quotes' and " . '"double quotes"
in it.';
echo $text."\n";
echo str_replace('"',"'",$text)."\n";
echo htmlentities($text,ENT_QUOTES);
?>
Yields:
php -q quotetest.php
This is a string with 'single quotes' and "double quotes" in it.
This is a string with 'single quotes' and 'double quotes' in it.
This is a string with 'single quotes' and &quot;double
quotes&quot; in it.

Ken

Jul 17 '05 #2
ah, that does it, thanks.
Could I use the same method to replace returns and newlines with <br>?

Jul 17 '05 #3
On 2005-01-19, Jakanapes <Ja*******@aol.com> wrote:
Could I use the same method to replace returns and newlines with <br>?


You would normally use nl2br() for that.

--
Cheers,
- Jacob Atzen
Jul 17 '05 #4
Not quite, doesn't nl2br still leave the /n?
I'm looking to replace /r and /n in strings with <br>

Jul 17 '05 #5
On 2005-01-19, Jakanapes <Ja*******@aol.com> wrote:
Not quite, doesn't nl2br still leave the /n?
I'm looking to replace /r and /n in strings with <br>


Yes, it does. You can eliminate those with:

str_replace("\r\n", "<br/>",$string);

The above will only replace a consecutive \r\n.

--
Cheers,
- Jacob Atzen
Jul 17 '05 #6
Jacob Atzen wrote:
str_replace("\r\n", "<br/>",$string);


That is better than nl2br in that it allows you to replace
newlines with '<BR>' instead of '<br />'.

--
Jock
Jul 17 '05 #7
John Dunlop wrote:
Jacob Atzen wrote:
str_replace("\r\n", "<br/>",$string);


That is better than nl2br in that it allows you to replace
newlines with '<BR>' instead of '<br />'.


Not true <http://in2.php.net/nl2br>

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #8
.oO(R. Rajesh Jeba Anbiah)
John Dunlop wrote:
Jacob Atzen wrote:
> str_replace("\r\n", "<br/>",$string);


That is better than nl2br in that it allows you to replace
newlines with '<BR>' instead of '<br />'.


Not true <http://in2.php.net/nl2br>


nl2br() returns XHTML since PHP 4.0.5, which is not always what you
want.

Micha
Jul 17 '05 #9
Jacob Atzen wrote:
On 2005-01-19, Jakanapes <Ja*******@aol.com> wrote:
Not quite, doesn't nl2br still leave the /n?
I'm looking to replace /r and /n in strings with <br>


Yes, it does. You can eliminate those with:

str_replace("\r\n", "<br/>",$string);

The above will only replace a consecutive \r\n.


both nl2br and str_replace have the disadvantage of not being MBCS (UTF-8
or other DBCS charsets) enabled.

ereg_replace would be the solution to use then ...

ereg_replace('"', "'", $string);
ereg_replace("'", '"', $string);

although it is likely much slower than both of these functions.

mark.
--
I am not an ANGRY man. Remove the rage from my email to reply.
Jul 17 '05 #10
R. Rajesh Jeba Anbiah wrote:
John Dunlop wrote:

Jacob Atzen wrote:
str_replace("\r\n", "<br/>",$string);


That is better than nl2br in that it allows you to replace
newlines with '<BR>' instead of '<br />'.


Not true <http://in2.php.net/nl2br>


You've stumped me, phpSt.Rajesh.

--
Jock
Jul 17 '05 #11
John Dunlop wrote:
R. Rajesh Jeba Anbiah wrote:
John Dunlop wrote:
Jacob Atzen wrote:
> str_replace("\r\n", "<br/>",$string);

That is better than nl2br in that it allows you to replace
newlines with '<BR>' instead of '<br />'.


Not true <http://in2.php.net/nl2br>


You've stumped me, phpSt.Rajesh.


Hey:-)

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Brian van den Broek | last post by:
Hi all, I'm posting partly so my problem and solution might be more easily found by google, and partly out of mere curiosity. I've just spent a frustrating bit of time figuring out why pydoc...
3
by: Kevin | last post by:
I know this has probably been discussed many times before (I found answers when I searched yesterday), but I still can't get it to work... I have an attribute @OID that can contain any...
3
by: Jason | last post by:
I have several tables with quite a few fields and I'm getting errors when trying to insert records with single quotes in the data like: name = John O'Henry or a city name of O'Fallen So I went...
7
by: gar | last post by:
Hi, I need to replace all the double quotes (") in a textbox with single quotes ('). I used this code text= Replace(text, """", "'" This works fine (for normal double quotes).The problem...
4
by: Justin Fancy | last post by:
Hi everyone, I need to replace all instances of a double quote(") with two single quotes('') in a text file. I already have some replacements of strings going on, but I tried this one, but the...
15
by: bill | last post by:
I am trying to write clean code but keep having trouble deciding when to quote an array index and when not to. sometimes when I quote an array index inside of double quotes I get an error about...
4
by: joecolson | last post by:
I've been trying to devise a JavaScript function that will take a GPS latitude or longitude coordinate expressed as degrees, minutes and seconds, and convert it to decimal degrees. Once the...
2
by: gimme_this_gimme_that | last post by:
var a = 'what goes in here to get a single quote?';
1
by: Yearwood | last post by:
Hi, I'm basically trying to import a CSV into an ACCESS database. Sample date is shown below: "",10173,"Development Manager - Social Economy Sector","Trust Bank",10153,,"Lolalll Pudd","Meet the...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.