Connecting Tech Pros Worldwide Forums | Help | Site Map

Replacing double quotes with single quotes

Jakanapes
Guest
 
Posts: n/a
#1: Jul 17 '05
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!


Ken Robinson
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Replacing double quotes with single quotes



Jakanapes wrote:[color=blue]
> Hi all,
>
> I'm looking for a way to scan a block of text and replace all the
> double quotes (") with single quotes (').
>[/color]

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

Jakanapes
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Replacing double quotes with single quotes


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

Jacob Atzen
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Replacing double quotes with single quotes


On 2005-01-19, Jakanapes <Jakanapes@aol.com> wrote:[color=blue]
> Could I use the same method to replace returns and newlines with <br>?[/color]

You would normally use nl2br() for that.

--
Cheers,
- Jacob Atzen
Jakanapes
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Replacing double quotes with single quotes


Not quite, doesn't nl2br still leave the /n?
I'm looking to replace /r and /n in strings with <br>

Jacob Atzen
Guest
 
Posts: n/a
#6: Jul 17 '05

re: Replacing double quotes with single quotes


On 2005-01-19, Jakanapes <Jakanapes@aol.com> wrote:[color=blue]
> Not quite, doesn't nl2br still leave the /n?
> I'm looking to replace /r and /n in strings with <br>[/color]

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
John Dunlop
Guest
 
Posts: n/a
#7: Jul 17 '05

re: Replacing double quotes with single quotes


Jacob Atzen wrote:
[color=blue]
> str_replace("\r\n", "<br/>",$string);[/color]

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

--
Jock
R. Rajesh Jeba Anbiah
Guest
 
Posts: n/a
#8: Jul 17 '05

re: Replacing double quotes with single quotes


John Dunlop wrote:[color=blue]
> Jacob Atzen wrote:
>[color=green]
> > str_replace("\r\n", "<br/>",$string);[/color]
>
> That is better than nl2br in that it allows you to replace
> newlines with '<BR>' instead of '<br />'.[/color]

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

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

Michael Fesser
Guest
 
Posts: n/a
#9: Jul 17 '05

re: Replacing double quotes with single quotes


.oO(R. Rajesh Jeba Anbiah)
[color=blue]
>John Dunlop wrote:[color=green]
>> Jacob Atzen wrote:
>>[color=darkred]
>> > str_replace("\r\n", "<br/>",$string);[/color]
>>
>> That is better than nl2br in that it allows you to replace
>> newlines with '<BR>' instead of '<br />'.[/color]
>
>Not true <http://in2.php.net/nl2br>[/color]

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

Micha
Mark
Guest
 
Posts: n/a
#10: Jul 17 '05

re: Replacing double quotes with single quotes


Jacob Atzen wrote:
[color=blue]
> On 2005-01-19, Jakanapes <Jakanapes@aol.com> wrote:[color=green]
>> Not quite, doesn't nl2br still leave the /n?
>> I'm looking to replace /r and /n in strings with <br>[/color]
>
> Yes, it does. You can eliminate those with:
>
> str_replace("\r\n", "<br/>",$string);
>
> The above will only replace a consecutive \r\n.
>[/color]

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.
John Dunlop
Guest
 
Posts: n/a
#11: Jul 17 '05

re: Replacing double quotes with single quotes


R. Rajesh Jeba Anbiah wrote:
[color=blue]
> John Dunlop wrote:[/color]
[color=blue][color=green]
> > Jacob Atzen wrote:[/color][/color]
[color=blue][color=green][color=darkred]
> > > str_replace("\r\n", "<br/>",$string);[/color]
> >
> > That is better than nl2br in that it allows you to replace
> > newlines with '<BR>' instead of '<br />'.[/color]
>
> Not true <http://in2.php.net/nl2br>[/color]

You've stumped me, phpSt.Rajesh.

--
Jock
R. Rajesh Jeba Anbiah
Guest
 
Posts: n/a
#12: Jul 17 '05

re: Replacing double quotes with single quotes


John Dunlop wrote:[color=blue]
> R. Rajesh Jeba Anbiah wrote:[color=green]
> > John Dunlop wrote:[color=darkred]
> > > 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 />'.[/color]
> >
> > Not true <http://in2.php.net/nl2br>[/color]
>
> You've stumped me, phpSt.Rajesh.[/color]

Hey:-)

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

Closed Thread