473,326 Members | 2,012 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,326 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 35981

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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.