📜 ⬆️ ⬇️

Simple things: Serialize an array in JavaScript for later deserialization in PHP.

Our next habratopic will be devoted to the serialization of arrays in JavaScript for subsequent deserialization in PHP . Thus, in our CMS S. Builder system , for example, it is implemented to save the settings of the visual editor interface — the user marks the toolbar buttons that need to be shown in the visual editor, indicates other settings, then presses Save. An array of settings is formed, then it is serialized and transmitted by AJAX to the server. On the server, the resulting string is written to the database. When displaying a toolbar, the PHP script accesses the database, pulls out a string, deserializes the array, and then displays the necessary buttons, etc.

The serialization function is quite simple and consists of several lines:

function sbSerialize (a) { var res = "" ; var total = 0; for ( var key in a) { total++; res += "s:" + String(key).length + ":\"" + String(key).replace(/ "/g, " \\\ "" ) + "\";s:" + String(a[key]).length + ":\"" + String(a[key]).replace(/ "/g, " \\\ "" ) + "\";" ; } res = "a:" + total + ":{" + res + "}" ; return res; } * This source code was highlighted with Source Code Highlighter .
  1. function sbSerialize (a) { var res = "" ; var total = 0; for ( var key in a) { total++; res += "s:" + String(key).length + ":\"" + String(key).replace(/ "/g, " \\\ "" ) + "\";s:" + String(a[key]).length + ":\"" + String(a[key]).replace(/ "/g, " \\\ "" ) + "\";" ; } res = "a:" + total + ":{" + res + "}" ; return res; } * This source code was highlighted with Source Code Highlighter .
  2. function sbSerialize (a) { var res = "" ; var total = 0; for ( var key in a) { total++; res += "s:" + String(key).length + ":\"" + String(key).replace(/ "/g, " \\\ "" ) + "\";s:" + String(a[key]).length + ":\"" + String(a[key]).replace(/ "/g, " \\\ "" ) + "\";" ; } res = "a:" + total + ":{" + res + "}" ; return res; } * This source code was highlighted with Source Code Highlighter .
  3. function sbSerialize (a) { var res = "" ; var total = 0; for ( var key in a) { total++; res += "s:" + String(key).length + ":\"" + String(key).replace(/ "/g, " \\\ "" ) + "\";s:" + String(a[key]).length + ":\"" + String(a[key]).replace(/ "/g, " \\\ "" ) + "\";" ; } res = "a:" + total + ":{" + res + "}" ; return res; } * This source code was highlighted with Source Code Highlighter .
  4. function sbSerialize (a) { var res = "" ; var total = 0; for ( var key in a) { total++; res += "s:" + String(key).length + ":\"" + String(key).replace(/ "/g, " \\\ "" ) + "\";s:" + String(a[key]).length + ":\"" + String(a[key]).replace(/ "/g, " \\\ "" ) + "\";" ; } res = "a:" + total + ":{" + res + "}" ; return res; } * This source code was highlighted with Source Code Highlighter .
  5. function sbSerialize (a) { var res = "" ; var total = 0; for ( var key in a) { total++; res += "s:" + String(key).length + ":\"" + String(key).replace(/ "/g, " \\\ "" ) + "\";s:" + String(a[key]).length + ":\"" + String(a[key]).replace(/ "/g, " \\\ "" ) + "\";" ; } res = "a:" + total + ":{" + res + "}" ; return res; } * This source code was highlighted with Source Code Highlighter .
  6. function sbSerialize (a) { var res = "" ; var total = 0; for ( var key in a) { total++; res += "s:" + String(key).length + ":\"" + String(key).replace(/ "/g, " \\\ "" ) + "\";s:" + String(a[key]).length + ":\"" + String(a[key]).replace(/ "/g, " \\\ "" ) + "\";" ; } res = "a:" + total + ":{" + res + "}" ; return res; } * This source code was highlighted with Source Code Highlighter .
  7. function sbSerialize (a) { var res = "" ; var total = 0; for ( var key in a) { total++; res += "s:" + String(key).length + ":\"" + String(key).replace(/ "/g, " \\\ "" ) + "\";s:" + String(a[key]).length + ":\"" + String(a[key]).replace(/ "/g, " \\\ "" ) + "\";" ; } res = "a:" + total + ":{" + res + "}" ; return res; } * This source code was highlighted with Source Code Highlighter .
  8. function sbSerialize (a) { var res = "" ; var total = 0; for ( var key in a) { total++; res += "s:" + String(key).length + ":\"" + String(key).replace(/ "/g, " \\\ "" ) + "\";s:" + String(a[key]).length + ":\"" + String(a[key]).replace(/ "/g, " \\\ "" ) + "\";" ; } res = "a:" + total + ":{" + res + "}" ; return res; } * This source code was highlighted with Source Code Highlighter .
  9. function sbSerialize (a) { var res = "" ; var total = 0; for ( var key in a) { total++; res += "s:" + String(key).length + ":\"" + String(key).replace(/ "/g, " \\\ "" ) + "\";s:" + String(a[key]).length + ":\"" + String(a[key]).replace(/ "/g, " \\\ "" ) + "\";" ; } res = "a:" + total + ":{" + res + "}" ; return res; } * This source code was highlighted with Source Code Highlighter .
  10. function sbSerialize (a) { var res = "" ; var total = 0; for ( var key in a) { total++; res += "s:" + String(key).length + ":\"" + String(key).replace(/ "/g, " \\\ "" ) + "\";s:" + String(a[key]).length + ":\"" + String(a[key]).replace(/ "/g, " \\\ "" ) + "\";" ; } res = "a:" + total + ":{" + res + "}" ; return res; } * This source code was highlighted with Source Code Highlighter .
  11. function sbSerialize (a) { var res = "" ; var total = 0; for ( var key in a) { total++; res += "s:" + String(key).length + ":\"" + String(key).replace(/ "/g, " \\\ "" ) + "\";s:" + String(a[key]).length + ":\"" + String(a[key]).replace(/ "/g, " \\\ "" ) + "\";" ; } res = "a:" + total + ":{" + res + "}" ; return res; } * This source code was highlighted with Source Code Highlighter .
  12. function sbSerialize (a) { var res = "" ; var total = 0; for ( var key in a) { total++; res += "s:" + String(key).length + ":\"" + String(key).replace(/ "/g, " \\\ "" ) + "\";s:" + String(a[key]).length + ":\"" + String(a[key]).replace(/ "/g, " \\\ "" ) + "\";" ; } res = "a:" + total + ":{" + res + "}" ; return res; } * This source code was highlighted with Source Code Highlighter .
function sbSerialize (a) { var res = "" ; var total = 0; for ( var key in a) { total++; res += "s:" + String(key).length + ":\"" + String(key).replace(/ "/g, " \\\ "" ) + "\";s:" + String(a[key]).length + ":\"" + String(a[key]).replace(/ "/g, " \\\ "" ) + "\";" ; } res = "a:" + total + ":{" + res + "}" ; return res; } * This source code was highlighted with Source Code Highlighter .

')
An array is passed to the function, which must be serialized, and a string is returned.

In PHP acc. to deserialize, just call unserialize. We hope someone will need it.

There is also an interesting example here:

kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_serialize

Source: https://habr.com/ru/post/41997/


All Articles