0

我似乎无法修复这个小精灵。

我有一个 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>

使用 ' 字符生成的数据输出:

<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() 来尝试解决问题,不确定我需要使用哪一个,有人能指出我正确的方向吗?

4

3 回答 3

1

您对嵌套引号的使用破坏了它。尝试这个:

<span class='showOnMap' data-location='<?php echo makeJSON($database_reelfilm, $reelfilm, "location", $row_rs_locations["id_loc"]);?>'>

通过在单引号中嵌套单引号,您可以结束字符串......在单引号和双引号之间切换将正确地将它们包含在完整字符串中。

于 2013-03-11T20:20:13.780 回答
0

您应该使用以下内容对值进行实体编码htmlentities()

<span class='showOnMap' data-location='<?php echo htmlentities(makeJSON($database_reelfilm, $reelfilm, 'location', $row_rs_locations['id_loc']));?>'>
于 2013-03-11T20:22:29.260 回答
0

有很多方法可以解决

使用 addlases 函数或使用 htmlspecialchars 函数

$myData['description']=htmlspecialchars(utf8_encode($row_rs_results['description_loc'],ENT_QUOTES));

                     OR
    $myData['description']=addslashes($myData['description']);
于 2015-09-09T13:29:49.563 回答