使用PHP转义变量中的引号 [英] Escape quotes in a variable with PHP

查看:58
本文介绍了使用PHP转义变量中的引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码来生成html

I use this code to genefate html

echo "<input type='button' onclick=\"myFunc('$param');\" />";

除非$ param包含'或字符。如何实现处理这些情况?

Everything would be OK unless $param contains ' or " character. How should it be implemented to handle these situations?

ps.mysql_real_escape_string($ param)当用户输入

ps. mysql_real_escape_string($param) won't work correctly, when a user entered ".

推荐答案

时,将无法正常工作有几个函数可以使用:

There are a couple of functions that could be used:

<?php
$string = 'string test"';

echo htmlentities($string) . "\n";
echo addslashes($string) . "\n";

他们产生以下结果:

string test&quot;
string test\"

这篇关于使用PHP转义变量中的引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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