We save json in shortcode attributes. We decided to use base64_encode to hide the square brackets, but we ran into some problems:
- Themecheck complains about base64 functions
- If you want to make a replacement in the database, for example. to replace all old site URLs with the new site URL, the regex cannot see what's inside the base64 encoded string
There is another solution using htmlentities
function encode($str) { $str = htmlentities($str, ENT_QUOTES, 'UTF-8'); // http://www.degraeve.com/reference/specialcharacters.php $special = array( '[' => '[', ']' => ']', ); $str = str_replace(array_keys($special), array_values($special), $str); return $str; } function decode($str) { return html_entity_decode($str, ENT_QUOTES, 'UTF-8'); } $original = '[1,2,3,"&",{a:1,b:2,"c":""}]'; $encoded = encode($original); $decoded = decode($encoded); echo "Original:\t", $original, PHP_EOL; echo "Shortcode:\t", '[hi abc="'. $encoded .'"]', PHP_EOL; echo "Decoded:\t", $decoded, PHP_EOL; echo "Equal:\t\t", ($original === $decoded) ? 'YES' : 'NO';
Output
Original: [1,2,3,"&",{a:1,b:2,"c":""}] Shortcode: [hi abc="[1,2,3,"&",{a:1,b:2,"c":""}]"] Decoded: [1,2,3,"&",{a:1,b:2,"c":""}] Equal: YES
http://ideone.com/fNiOkD