» Javascript equivalent for PHP's serialize
134 PHP equivalents
- PHP.JS Licensing
- PHP.JS SVN
- PHP.JS Namespaced
- abs
- addslashes
- array_change_key_case
- array_chunk
- array_combine
- array_count_values
- array_diff
- array_diff_assoc
- array_diff_key
- array_fill
- array_flip
- array_keys
- array_key_exists
- array_map
- array_pad
- array_pop
- array_product
- array_push
- array_rand
- array_reduce
- array_reverse
- array_search
- array_shift
- array_sum
- array_unique
- array_unshift
- array_values
- base64_decode
- base64_encode
- basename
- bin2hex
- call_user_func_array
- checkdate
- chr
- compact
- count
- count_chars
- crc32
- date
- defined
- dirname
- echo
- empty
- end
- explode
- file
- file_get_contents
- floatval
- function_exists
- get_class
- get_included_files
- htmlentities
- html_entity_decode
- http_build_query
- implode
- include
- include_once
- intval
- in_array
- ip2long
- isset
- is_array
- is_null
- is_numeric
- is_object
- is_string
- join
- levenshtein
- long2ip
- ltrim
- md5
- md5_file
- mktime
- nl2br
- number_format
- ord
- parse_str
- preg_quote
- printf
- print_r
- rand
- range
- require
- require_once
- reset
- round
- rtrim
- » serialize
- setcookie
- sha1
- sha1_file
- shuffle
- sizeof
- soundex
- split
- sprintf
- strcasecmp
- strchr
- strcmp
- stripos
- stripslashes
- strip_tags
- stristr
- strlen
- strnatcmp
- strpbrk
- strpos
- strrev
- strripos
- strrpos
- strstr
- strtolower
- strtoupper
- str_ireplace
- str_pad
- str_repeat
- str_replace
- str_rot13
- str_split
- substr
- substr_count
- trim
- ucfirst
- ucwords
- unserialize
- urldecode
- urlencode
- utf8_decode
- utf8_encode
- var_export
- wordwrap
PHP to Javascript Project: php.js
This article is part of the 'Porting PHP to Javascript' Project, which aims to decrease the gap between developing for PHP & Javascript.
A lot of people are familiar with PHP's functions, and though Javascript functions are often quite similar, some functions may be missing or addressed differently. The Javascript implementations should be as compliant with the PHP versions as possible, a good indication is that the PHP function manual could also apply to the Javascript version.
Porting crucial PHP functions to Javascript can be fun & useful. Currently some PHP functions have been added, but readers are encouraged to contribute and improve functions by adding comments. Eventually the goal is to save all the functions in one php.js file and make it publicly available for your coding pleasure.
If you choose to contribute, let me know how you want to be credited in the function's comments. You may also want to subscribe to RSS so you receive updates whenever new functions are posted.
This is a Javascript version of the PHP function: serialize.
PHP serialize
Description
serialize - Generates a storable representation of a value
string serialize ( mixed value)
Generates a storable representation of a value
Parameters
-
value
The value to be serialized. serialize() handles all types, except the resource-type. You can even serialize() arrays that contain references to itself. Circular references inside the array/object you are serialize()ing will also be stored. Any other reference will be lost.
When serializing objects, PHP will attempt to call the member function __sleep() prior to serialization. This is to allow the object to do any last minute clean-up, etc. prior to being serialized. Likewise, when the object is restored using unserialize() the __wakeup() member function is called.
Return Values
Returns a string containing a byte-stream representation of value that can be stored anywhere.
See Also
Javascript serialize
Source
This is the main source of the Javascript version of PHP's serialize
function serialize( inp ) { // http://kevin.vanzonneveld.net // + original by: Arpad Ray (mailto:arpad@php.net) // * example 1: serialize(['Kevin', 'van', 'Zonneveld']); // * returns 1: 'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}' var getType = function( inp ) { var type = typeof inp, match; if(type == 'object' && !inp) { return 'null'; } if (type == "object") { if(!inp.constructor) { return 'object'; } var cons = inp.constructor.toString(); if (match = cons.match(/(\w+)\(/)) { cons = match[1].toLowerCase(); } var types = ["boolean", "number", "string", "array"]; for (key in types) { if (cons == types[key]) { type = types[key]; break; } } } return type; }; var type = getType(inp); var val; switch (type) { case "undefined": val = "N"; break; case "boolean": val = "b:" + (inp ? "1" : "0"); break; case "number": val = (Math.round(inp) == inp ? "i" : "d") + ":" + inp; break; case "string": val = "s:" + inp.length + ":\"" + inp + "\""; break; case "array": val = "a"; case "object": if (type == "object") { var objname = inp.constructor.toString().match(/(\w+)\(\)/); if (objname == undefined) { return; } objname[1] = serialize(objname[1]); val = "O" + objname[1].substring(1, objname[1].length - 1); } var count = 0; var vals = ""; var okey; for (key in inp) { okey = (key.match(/^[0-9]+$/) ? parseInt(key) : key); vals += serialize(okey) + serialize(inp[key]); count++; } val += ":" + count + ":{" + vals + "}"; break; } if (type != "object" && type != "array") val += ";"; return val; }
Examples
Currently there is 1 example
Example 1
serialize(['Kevin', 'van', 'Zonneveld']);And that would return
'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}'More about this Project
Download php.js
To easily include it in your code, every function currently available is stored in
Normal
- uncompressed source: php.js (147.1kB)
- minified: php.min.js (54.1kB)
- compressed: php.packed.js (39kB)
Namespaced What is 'namespaced?'
- uncompressed source: php.namespaced.js (181.6kB)
- minified: php.namespaced.min.js (54.5kB)
- compressed: php.namespaced.packed.js (39.4kB)
To download use Right click, Save Link As
Generally the best way is to use a minified version and gzip it
Testing the functions
The number of functions is growing fast and so it becomes hard to maintain quality.
To defeat that danger of bad code, syntax errors, etc, I've added a new feature: php.js tester.
It is an automatically generated page that includes ALL functions in your browser, and then extracts specific testing information from each function's comments. This info is then used to run the function, and the return value is compared to a predefined one.
This way code is always checked on syntax errors, and if it doesn't function correctly anymore after an update, we should also be able to detect it more easily.
If you want, go check it out.
Credits
Respect & awards go to everybody who has contributed in some way so far:
![]() ![]() | Michael White (link) for contributing to: |
| array_count_values, get_included_files, include, include_once, require, require_once, md5, number_format, parse_str, printf, sha1, sprintf, str_pad, strnatcmp, http_build_query, floatval, is_object, print_r | |
![]() ![]() | _argos for contributing to: |
| array_fill, array_pad, array_product, array_rand, compact, count, range, abs, defined, ip2long, long2ip, implode, strcmp, ucwords | |
![]() ![]() | Jonas Raoni Soares Silva (link) for contributing to: |
| shuffle, abs, setcookie, number_format, number_format, soundex, str_repeat, str_replace, str_rot13, ucwords, wordwrap, wordwrap | |
![]() ![]() | Legaev Andrey for contributing to: |
| end, reset, file, file_get_contents, function_exists, include, include_once, http_build_query, is_array, is_object | |
![]() ![]() | Ates Goral (link) for contributing to: |
| array_change_key_case, array_count_values, array_diff_key, get_class, preg_quote, addslashes, count_chars, str_rot13, stripslashes | |
![]() ![]() | Philip Peterson for contributing to: |
| sizeof, round, echo, nl2br, str_replace, strchr, urldecode, urlencode, var_export | |
![]() ![]() | Martijn Wieringa for contributing to: |
| str_ireplace, str_split, strcasecmp, stripos, strnatcmp, substr | |
![]() ![]() | Webtoolkit.info (link) for contributing to: |
| crc32, md5, sha1, utf8_decode, utf8_encode | |
![]() ![]() | Carlos R. L. Rodrigues (link) for contributing to: |
| array_chunk, array_unique, date, levenshtein | |
![]() ![]() | Ash Searle (link) for contributing to: |
| basename, printf, sprintf | |
![]() ![]() | Erkekjetter for contributing to: |
| ltrim, rtrim, trim | |
![]() ![]() | marrtins for contributing to: |
| array_change_key_case, addslashes, stripslashes | |
![]() ![]() | Alfonso Jimenez (link) for contributing to: |
| array_reduce, strpbrk | |
![]() ![]() | Aman Gupta for contributing to: |
| base64_decode, utf8_decode | |
![]() ![]() | Arpad Ray (mailto:arpad@php.net) for contributing to: |
| serialize, unserialize | |
![]() ![]() | Karol Kowalski for contributing to: |
| array_reverse, abs | |
![]() ![]() | Thunder.m for contributing to: |
| base64_decode, base64_encode | |
![]() ![]() | Tyler Akins (link) for contributing to: |
| base64_decode, base64_encode | |
![]() ![]() | mdsjack (link) for contributing to: |
| include, trim | |
![]() ![]() | Alexander Ermolaev (link) for contributing to: |
| trim | |
![]() ![]() | Allan Jensen (link) for contributing to: |
| number_format | |
![]() ![]() | Andrea Giammarchi (link) for contributing to: |
| array_map | |
![]() ![]() | Bayron Guevara for contributing to: |
| base64_encode | |
![]() ![]() | Benjamin Lupton for contributing to: |
| number_format | |
![]() ![]() | Brad Touesnard for contributing to: |
| date | |
![]() ![]() | Brett Zamir for contributing to: |
| str_split | |
![]() ![]() | Cagri Ekin for contributing to: |
| parse_str | |
![]() ![]() | Cord for contributing to: |
| is_array | |
![]() ![]() | David for contributing to: |
| is_numeric | |
![]() ![]() | David James for contributing to: |
| get_class | |
![]() ![]() | DxGx for contributing to: |
| trim | |
![]() ![]() | FGFEmperor for contributing to: |
| mktime | |
![]() ![]() | Felix Geisendoerfer (link) for contributing to: |
| array_key_exists | |
![]() ![]() | FremyCompany for contributing to: |
| isset | |
![]() ![]() | Gabriel Paderni for contributing to: |
| str_replace | |
![]() ![]() | Leslie Hoare for contributing to: |
| rand | |
![]() ![]() | Lincoln Ramsay for contributing to: |
| basename | |
![]() ![]() | MeEtc (link) for contributing to: |
| date | |
![]() ![]() | Mick@el for contributing to: |
| stripslashes | |
![]() ![]() | Nick Callen for contributing to: |
| wordwrap | |
![]() ![]() | Ozh for contributing to: |
| dirname | |
![]() ![]() | Pedro Tainha (link) for contributing to: |
| unserialize | |
![]() ![]() | Peter-Paul Koch (link) for contributing to: |
| date | |
![]() ![]() | Philippe Baumann for contributing to: |
| empty | |
![]() ![]() | Sakimori for contributing to: |
| strlen | |
![]() ![]() | Sanjoy Roy for contributing to: |
| array_diff | |
![]() ![]() | Simon Willison (link) for contributing to: |
| str_replace | |
![]() ![]() | Steve Clay for contributing to: |
| function_exists | |
![]() ![]() | Steve Hilder for contributing to: |
| strcmp | |
![]() ![]() | Steven Levithan (link) for contributing to: |
| trim | |
![]() ![]() | T0bsn for contributing to: |
| crc32 | |
![]() ![]() | Thiago Mata (link) for contributing to: |
| call_user_func_array | |
![]() ![]() | Tim Wiel for contributing to: |
| date | |
![]() ![]() | XoraX (link) for contributing to: |
| dirname | |
![]() ![]() | baris ozdil for contributing to: |
| mktime | |
![]() ![]() | booeyOH for contributing to: |
| preg_quote | |
![]() ![]() | djmix for contributing to: |
| basename | |
![]() ![]() | duncan for contributing to: |
| array_unique | |
![]() ![]() | echo is bad for contributing to: |
| echo | |
![]() ![]() | gabriel paderni for contributing to: |
| mktime | |
![]() ![]() | ger for contributing to: |
| html_entity_decode | |
![]() ![]() | john (link) for contributing to: |
| html_entity_decode | |
![]() ![]() | kenneth for contributing to: |
| explode | |
![]() ![]() | penutbutterjelly for contributing to: |
| str_ireplace | |
![]() ![]() | stensi for contributing to: |
| intval | |
Your name here?
Contributing is as easy as adding a comment with better code, or code for a new function.
Any contribution leading to improvement will directly get your name & link here.
Coming Project features
Project features that we are currently working on:
- Versioning. Individual functions are versioned, but the entire library should be versioned as well.
- Light. A lightweight version of php.js should be made available with only common functions in it.
Like this article?
|
Then Dzone it! Or use another bookmark button below to show your support & help me spread the word. |
Hot StuffFlaming articles» Survive heavy traffic with you... | RelatedArticles like this one» Javascript equivalent for PHP'... |
tags: programming, php, javascript
category: Programming - Javascript - PHP equivalents
read: 2,537 times










tagcloud
#13. Kevin on 02 March 2008
#12. Andrea Giammarchi on 02 March 2008
http://www.devpro.it/javascript_id_102.html
It's able to save correctly UTF-8 strings as well.
... [more] Cheers
#11. Kevin on 02 March 2008
#10. Kevin on 28 February 2008
#9. Doug on 28 February 2008
#8. Franck Chionna on 20 February 2008
i d like to serialize a window object by a js var that contain window.open , thus to keep in memory the window open if the php page is refreshed. i tried to use your code but it says js error "too much recursion... any suggestion ? thanks and congratulation for the work done
#7. Ates Goral on 23 January 2008
#6. Kevin on 23 January 2008
#5. Kevin on 23 January 2008
About your php-strict/javascript-flexible question. I think we should stay with PHP as close as possible. Hopefully this will provide consistency & clarity for end users. And interoperability between php-js-function throughout the project. This approach should also ensure that no extra function documentation has to be written because PHP's function manual will (in most cases) be valid.
#4. Ates Goral on 22 January 2008
#3. Kevin on 22 January 2008
#2. Ates Goral on 21 January 2008
#1. Ates Goral on 21 January 2008
Hi Kevin,
Here are a few improvements to what I originally had:
For Array detection, instead of:
it's nicer to say: Also, an additional check can be added to handle the NaN and Infinite values: