» Javascript equivalent for PHP's urlencode
140 PHP equivalents
- PHP.JS Licensing
- PHP.JS SVN
- PHP.JS Namespaced
- abs
- addslashes
- array
- 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
- htmlspecialchars
- htmlspecialchars_decode
- html_entity_decode
- http_build_query
- implode
- include
- include_once
- intval
- in_array
- ip2long
- isset
- is_array
- is_int
- 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
- rsort
- rtrim
- serialize
- setcookie
- sha1
- sha1_file
- shuffle
- sizeof
- sort
- 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: urlencode.
PHP urlencode
Description
urlencode - URL-encodes string
string urlencode( string str )
strThe string to be encoded.
Parameters
-
str
The string to be encoded.
Return Values
Returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs. It is encoded the same way that the posted data from a WWW form is encoded, that is the same way as in application/x-www-form-urlencoded media type. This differs from the ยป RFC 1738 encoding (see rawurlencode()) in that for historical reasons, spaces are encoded as plus (+) signs.
See Also
Javascript urlencode
Source
This is the main source of the Javascript version of PHP's urlencode
function urlencode( str ) { // http://kevin.vanzonneveld.net // + original by: Philip Peterson // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // * example 1: urlencode('Kevin van Zonneveld!'); // * returns 1: 'Kevin+van+Zonneveld%21' var ret = str; ret = ret.toString(); ret = encodeURIComponent(ret); ret = ret.replace(/%20/g, '+'); return ret; }
Examples
Currently there is 1 example
Example 1
urlencode('Kevin van Zonneveld!');And that would return
'Kevin+van+Zonneveld%21'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 (152kB)
- minified: php.min.js (55kB)
- compressed: php.packed.js (39.8kB)
Namespaced What is 'namespaced?'
- uncompressed source: php.namespaced.js (187.6kB)
- minified: php.namespaced.min.js (55.4kB)
- compressed: php.namespaced.packed.js (40.1kB)
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:
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» PHP.JS Licensing |
tags: programming, php, javascript, phpjs
category: Programming - Javascript - PHP equivalents
read: 3,994 times










tagcloud
#3. Kevin on 18 April 2008
I guess the tester doesn't work because in it current form it fails to handle \n characters, and maybe the exclamation mark gets translated twice, I have to double check that.
Discussion on encodeURIComponent vs escape can be found here:
... [more] http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_http_build_query/#comment_1071
If you reach a different conclusion, please let me know ok?
#2. Philip Peterson on 18 April 2008
#1. Philip Peterson on 18 April 2008