用单引号替换双引号 [英] Replacing double quotes with single quotes

查看:166
本文介绍了用单引号替换双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在寻找一种方法来扫描一段文字并替换所有

双引号(")单引号('')。


我正在使用PHP从mySQL表中提取文本,然后将文本

输入到javascript中onClick调用的函数。问题是

文本可能包含单引号或双引号,这搞砸了

javascript。我通过addslashes运行文本

消除了单引号问题,但双引号是个问题。

这是一个例子。


mySQL文字:

父亲的字样对吸收,关注和对婴儿的兴趣

$ 225美元以后的数字:

\父亲的'\\'\吸收,关注和对婴儿的兴趣


试图在onClick上使用:

onclick =" printAnswer(''the \father\ ''吸收,专注和

对婴儿的兴趣'')
javascript控制台出错:

错误:未终止的字符串文字

源代码:

printAnswer(''\


任何提示或建议都会非常欢迎!

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!

推荐答案



Jakanapes写道:

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 ('').




嗯,您可以使用str_replace('''''',''''',



Well, you could use str_replace(''"'',"''",


文本)或使用

htmlentities(
text) or use
htmlentities(


text,ENT_QUOTES)。


示例:

< ;?
text,ENT_QUOTES).

Example:
<?


这篇关于用单引号替换双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆