我似乎无法修复这个小小的gremlin。
我有一个mySQL数据库,该表有一个描述字段。
我的客户已经使用了'字符很多,这打破了我从php生成的json。
我的脚本如下:
... database call...
$totalRows_rs_results = mysql_num_rows($rs_results);
if($totalRows_rs_results >0){
$myData = array();
//echo('locations near this unitbase:');
$myData['id'] = $row_rs_results['id_loc'];
$myData['slug'] = $row_rs_results['slug_loc'];
$myData['name'] = $row_rs_results['name_loc'];
$myData['showname'] = $row_rs_results['showname_loc'];
$myData['description'] = utf8_encode($row_rs_results['description_loc']);
$myData['image'] = getDefaultLocationImage($database_reelfilm, $reelfilm, $row_rs_results['id_loc']);
$myAddress = ($row_rs_results['address_loc'].' '.$row_rs_results['postcode_loc']);
$myData['address'] = $myAddress;
$myData['maplat'] = $row_rs_results['maplat_loc'];
$myData['maplong'] = $row_rs_results['maplong_loc'];
$myData['parking'] = $row_rs_results['parkinginfo_loc'];
$myData['features'] = $row_rs_results['internalfeatures_loc'] . ' ' . $row_rs_results['externalfeatures_loc'];
//$myData['categorys'] = getCategorys($database_reelfilm, $reelfilm, $row_rs_results['id_loc']);
$json[] = $myData;
$myJSON = json_encode($json);
return $myJSON;
};
<span class='showOnMap' data-location='<?php echo makeJSON($database_reelfilm, $reelfilm, 'location', $row_rs_locations['id_loc']);?>' '>
<img src="/images/Icons/map-icon.png" width="32" height="32">
</span>
输出的json很好,直到我用前面提到的'字符来记录。
如果我从记录中删除有问题的'字符,我得到正确的输出:
<span class="showOnMap" data-related="" data-categorys="" data-location="[{"id":"29","slug":"the-butts","name":"The Butts","showname":"1","description":"Controllable period street location. Looks like an A road or a bus route. Surrounding houses are Georgian or early Victorian.","image":"The Butts 3.jpg","address":"Brentford\r\nLondon TW8 8BQ","maplat":"51.4847373","maplong":"-0.30824250000000575","parking":"","features":" "}]">
<img width="32" height="32" src="/images/Icons/map-icon.png">
</span>
使用'character:
生成的数据输出<span class="showOnMap" data-related="" data-categorys="" "}]'="" 8bq","maplat":"51.4847373","maplong":"-0.30824250000000575","parking":"","features":"="" tw8="" 3.jpg","address":"brentford\r\nlondon="" butts="" victorian.","image":"the="" early="" georgian="" are="" houses="" surrounding="" route.="" bus="" a="" or="" road="" a'="" data-location="[{"id":"29","slug":"the-butts","name":"The Butts","showname":"1","description":"Controllable period street location. Looks like an ">
<img width="32" height="32" src="/images/Icons/map-icon.png">
</span>
现在告诉我的客户通过并删除所有违规的字符(超过1000条记录),这对我来说似乎非常不专业。
我试过htmlspecialchars(),utf8_encode()试图解决问题,不确定我需要使用哪一个,是否有人能指出我正确的方向?
答案 0 :(得分:1)
您使用嵌套引号会破坏它。试试这个:
<span class='showOnMap' data-location='<?php echo makeJSON($database_reelfilm, $reelfilm, "location", $row_rs_locations["id_loc"]);?>'>
通过在单引号中嵌套单引号,您可以结束字符串...在单引号和双引号之间切换将正确地将它们包含在完整字符串中。
答案 1 :(得分:0)
您应该使用htmlentities()
对值进行实体编码,例如:
<span class='showOnMap' data-location='<?php echo htmlentities(makeJSON($database_reelfilm, $reelfilm, 'location', $row_rs_locations['id_loc']));?>'>
答案 2 :(得分:0)
有很多方法可以解决
使用addslases函数或使用htmlspecialchars函数
$myData['description']=htmlspecialchars(utf8_encode($row_rs_results['description_loc'],ENT_QUOTES));
OR
$myData['description']=addslashes($myData['description']);