你如何处理',在php,javascript中人名中的单引号 [英] How do you deal with ', single quote in person's names in php, javascript
问题描述
我注意到某些名称条目(例如 O'Rourke 或 O'reilly)在某些情况下会破坏 javascript.我想知道你是怎么处理的.我从 PHP/MYSQL 中提取这些名称并将它们放入 javascript 中.
I noticed that some of name entries, like O'Rourke or O'reilly breaks javascript in some cases. I wonder how you deal with it. I pull these names out of PHP/MYSQL and put them in javascript.
你是在进入数据库之前还是在退出数据库之后替换它们?
Do you replace them before entering to database or after pulling out of database?
提前致谢
google.load('visualization', '1', {packages:['table']});
google.setOnLoadCallback(drawTable);
function drawTable() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'First Name');
data.addColumn('string', 'Last Name');
data.addColumn('string', 'email');
data.addColumn('string', 'Parent email 1');
data.addColumn('string', 'Parent email 2');
data.addColumn('string', 'Advisor');
data.addColumn('string', 'Active');
data.addColumn('string', 'Edit');
//data.addColumn('number', '');
data.addRows([
<?php
if(count($items))
{
foreach($items as $item)
{
foreach($advisors as $key=>$advisor)
{
if($item['advisor']==$advisor['id'])
{
$ad=$advisor['last_name'];
}
}
$active_icon = ($item['active']=='1'?'tick':'cross');
$editlink = anchor('auth/admin/members/form/'.$item['id'],$this->bep_assets->icon('pencil'));
echo "['".$item['first_name']."', '".$item['last_name']."', '".$item['email']
."', '".$item['parent_email1']."', '".$item['parent_email2']."', '".$ad."', '".$this->bep_assets->icon($active_icon)."','".$editlink."'],";
}
}
?>
]);
var table = new google.visualization.Table(document.getElementById('table_div'));
table.draw(data, {showRowNumber: true, allowHtml:true});
}
推荐答案
为了输出一个 javascript 变量,您正在经历大量混乱且容易出错的字符串附加工作.你应该做的是在 PHP 中构建你想要的数据结构(一个数组数组),然后使用 json_encode()
来发出一个与 javascript 兼容的文字.所有引号等都会被编码器自动转义.
You're going through a lot of messy and error-prone string-appending work just to output a javascript variable. What you should do instead is build the data structure you want in PHP (an array of arrays) and then use json_encode()
to emit a javascript-compatible literal. All quotes and such will be automatically escaped by the encoder.
$itemOutput = array();
if(count($items)) {
foreach($items as $item) {
foreach($advisors as $key=>$advisor) {
if($item['advisor']==$advisor['id']) {
$ad=$advisor['last_name'];
}
}
$active_icon = ($item['active']=='1'?'tick':'cross');
$editlink = anchor('auth/admin/members/form/'.$item['id'],$this->bep_assets->icon('pencil'));
$itemOutput[] = array(
$item['first_name'],
$item['last_name'],
$item['email'],
$item['parent_email'],
$item['parent_email2'],
$ad,
$this->beep_assets->icon($active_icon),
$editLink
);
}
}
echo "data.addRows(" . json_encode($itemOutput) . ");" ;
如果你有一个对象或一个关联数组,它会作为一个 javascript 对象发出:
If you have an object or an associative array, it gets emitted as a javascript object:
echo json_encode(
array( 'a'=>'aa', 'b'=>'bb'),
array( 'c'=>'cc', 'd'=>'dd')
);
==> [{"a":"aa","b":"bb"},{"c":"cc","d":"dd"}]
这篇关于你如何处理',在php,javascript中人名中的单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!