Connecting Tech Pros Worldwide Help | Site Map

Replacing double quotes with single quotes

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 10:48 AM
Jakanapes
Guest
 
Posts: n/a
Default 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!


  #2  
Old July 17th, 2005, 10:48 AM
Ken Robinson
Guest
 
Posts: n/a
Default 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

  #3  
Old July 17th, 2005, 10:49 AM
Jakanapes
Guest
 
Posts: n/a
Default 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>?

  #4  
Old July 17th, 2005, 10:49 AM
Jacob Atzen
Guest
 
Posts: n/a
Default 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
  #5  
Old July 17th, 2005, 10:49 AM
Jakanapes
Guest
 
Posts: n/a
Default 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>

  #6  
Old July 17th, 2005, 10:49 AM
Jacob Atzen
Guest
 
Posts: n/a
Default 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
  #7  
Old July 17th, 2005, 10:49 AM
John Dunlop
Guest
 
Posts: n/a
Default 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
  #8  
Old July 17th, 2005, 10:49 AM
R. Rajesh Jeba Anbiah
Guest
 
Posts: n/a
Default 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/

  #9  
Old July 17th, 2005, 10:49 AM
Michael Fesser
Guest
 
Posts: n/a
Default 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
  #10  
Old July 17th, 2005, 10:49 AM
Mark
Guest
 
Posts: n/a
Default 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.
  #11  
Old July 17th, 2005, 10:49 AM
John Dunlop
Guest
 
Posts: n/a
Default nl2br

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
  #12  
Old July 17th, 2005, 11:07 AM
R. Rajesh Jeba Anbiah
Guest
 
Posts: n/a
Default [OT] Re: nl2br

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/

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

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 220,989 network members.