php - How to store this string as valid json in mysql (utf8_encode and htmlentities not helping) -
here string trying store
<div id="site-summary"> <p> <strong>welcome whiteley helyar.</strong> independent, friendly , professional, seeking achieve best our clients. whiteley helyar 1 of bath’s leading residential estate agencies , deal in sales – nothing else. our many satisfied customers value our efficiency, honesty , expert understanding of property in bath , surrounding villages. not find knowledgeable , helpful, eager , progressive in our approach. if house-hunting in 1 of uk’s beautiful , vibrant cities, our experienced team of partners , negotiators can find perfect property. </p> </div>
there unusual characters in here causing json_encode not encode string json when it's in array.
here code $about_us above string
$about_us = utf8_encode($about_us); $about_us = htmlentities($about_us, ent_quotes | ent_ignore, "utf-8"); $about_us = mysql_real_escape_string($about_us); $link_array = array(); $link_array['stuff'] = 'other string'; $link_array['about_us'] = $about_us; $json= json_encode($link_array);
i store $json in mysql text field. however. can see examining $json string it's not valid json , fails on http://jsonlint.com/. , when json_decode stuff works correctly empty.
this in database about_us property:
<div id="site-summary">\r\n <p><strong>welcome whiteley helyar.</strong> independent, friendly , professional, seeking achieve best our clients. whiteley helyar 1 of bathâu0080u0099s leading residential estate agencies , deal in sales âu0080u0093 nothing else. our many satisfied customers value our efficiency, honesty , expert understanding of property in bath , surrounding villages. not find knowledgeable , helpful, eager , progressive in our approach. if house-hunting in 1 of ukâu0080u0099s beautiful , vibrant cities, our experienced team of partners , negotiators can find perfect property.</p>\r\n </div>
any ideas please
edit: different linked question since utf8_encode not helping here (and accepted answer question).
first of all, if you're still using mysql_real_escape_string, you're not using pdo interact database. should using pdo @ point.
if use pdo, solves lot of problems , simple this:
$db = new pdo('mysql:host=change_this_to_your_host_name; dbname=change_this_to_your_database', 'change_this_to_your_username', 'change_this_to_your_password'); $about_us = '<div id="site-summary"> <p><strong>welcome whiteley helyar.</strong> independent, friendly , professional, seeking achieve best our clients. whiteley helyar 1 of bath’s leading residential estate agencies , deal in sales – nothing else. our many satisfied customers value our efficiency, honesty , expert understanding of property in bath , surrounding villages. not find knowledgeable , helpful, eager , progressive in our approach. if house-hunting in 1 of uk’s beautiful , vibrant cities, our experienced team of partners , negotiators can find perfect property.</p> </div>'; $link_array = array(); $link_array['stuff'] = 'other string'; $link_array['about_us'] = $about_us; $json= json_encode($link_array); $query = $db->prepare("insert settings (json) values (:json) "); $query->bindvalue(':json', $json, pdo::param_str); $query->execute();
then, if in database , copy , paste what's in json field jsonlint, validate fine. plus, way, html readable in database (rather having store <
, "
, such).
Comments
Post a Comment