fork(2) download
  1. <?php
  2.  
  3. function encode($str) {
  4. $str = htmlentities($str, ENT_QUOTES, 'UTF-8');
  5.  
  6. // http://w...content-available-to-author-only...e.com/reference/specialcharacters.php
  7. $special = array(
  8. '[' => '&#91;',
  9. ']' => '&#93;',
  10. );
  11. $str = str_replace(array_keys($special), array_values($special), $str);
  12.  
  13. return $str;
  14. }
  15.  
  16. function decode($str) {
  17. return html_entity_decode($str, ENT_QUOTES, 'UTF-8');
  18. }
  19.  
  20. $original = '[1,2,3,"&",{a:1,b:2,"c":"Результат"}]';
  21. $encoded = encode($original);
  22. $decoded = decode($encoded);
  23.  
  24. echo "Original:\t", $original, PHP_EOL;
  25. echo "Shortcode:\t", '[hi abc="'. $encoded .'"]', PHP_EOL;
  26. echo "Decoded:\t", $decoded, PHP_EOL;
  27. echo "Equal:\t\t", ($original === $decoded) ? 'YES' : 'NO';
  28.  
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
Original:	[1,2,3,"&",{a:1,b:2,"c":"Результат"}]
Shortcode:	[hi abc="&#91;1,2,3,&quot;&amp;&quot;,{a:1,b:2,&quot;c&quot;:&quot;Результат&quot;}&#93;"]
Decoded:	[1,2,3,"&",{a:1,b:2,"c":"Результат"}]
Equal:		YES