Replacing double quotes with single quotes 
July 17th, 2005, 11:48 AM
| | | |
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! | 
July 17th, 2005, 11:48 AM
| | | | 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 "double
quotes" in it.
Ken | 
July 17th, 2005, 11:49 AM
| | | | 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>? | 
July 17th, 2005, 11:49 AM
| | | | 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 | 
July 17th, 2005, 11:49 AM
| | | | 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> | 
July 17th, 2005, 11:49 AM
| | | | 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 | 
July 17th, 2005, 11:49 AM
| | | | 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 | 
July 17th, 2005, 11:49 AM
| | | | 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/ | 
July 17th, 2005, 11:49 AM
| | | | 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 | 
July 17th, 2005, 11:49 AM
| | | | 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. | 
July 17th, 2005, 11:49 AM
| | | | 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 | 
July 17th, 2005, 12:07 PM
| | | | 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/ |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|