By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,534 Members | 1,830 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,534 IT Pros & Developers. It's quick & easy.

Replacing double quotes with single quotes

P: n/a
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
Share this Question
Share on Google+
11 Replies


P: n/a

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

P: n/a
ah, that does it, thanks.
Could I use the same method to replace returns and newlines with <br>?

Jul 17 '05 #3

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
.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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.