» Javascript equivalent for PHP's strnatcmp
146 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
- array_walk
- array_walk_recursive
- base64_decode
- base64_encode
- basename
- bin2hex
- call_user_func_array
- checkdate
- chr
- compact
- count
- count_chars
- crc32
- create_function
- 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
- krsort
- ksort
- 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
- time
- 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: strnatcmp.
PHP strnatcmp
Description
strnatcmp - String comparisons using a "natural order" algorithm
int strnatcmp( string str1 , string str2 )
This function implements a comparison algorithm that orders alphanumeric strings in the way a human being would, this is described as a "natural ordering". Note that this comparison is case sensitive.
See Also
- javascript strcasecmp()
- javascript substr()
- javascript stristr()
- javascript strcmp()
- javascript strstr()
Javascript strnatcmp
Source
This is the main source of the Javascript version of PHP's strnatcmp
function strnatcmp ( f_string1, f_string2, f_version ) { // http://kevin.vanzonneveld.net // + original by: Martijn Wieringa // + namespaced by: Michael White (http://crestidg.com) // - depends on: strcmp // % note: Added f_version argument against code guidelines, because it's so neat // * example 1: strnatcmp('Price 12.9', 'Price 12.15'); // * returns 1: 1 // * example 2: strnatcmp('Price 12.09', 'Price 12.15'); // * returns 2: -1 // * example 3: strnatcmp('Price 12.90', 'Price 12.15'); // * returns 3: 1 // * example 4: strnatcmp('Version 12.9', 'Version 12.15', true); // * returns 4: -6 // * example 5: strnatcmp('Version 12.15', 'Version 12.9', true); // * returns 5: 6 if(f_version == undefined) { f_version = false; } var __strnatcmp_split = function( f_string ) { var result = new Array(); var buffer = ''; var chr = ''; var text = true; for(var i = 0; i < f_string.length; i++){ chr = f_string.substring(i, i + 1); if(chr.match(/[0-9]/)){ if(text){ if(buffer.length > 0){ result[result.length] = buffer; buffer = ''; } text = false; } buffer += chr; } else if((text == false) && (chr == '.') && (i < (f_string.length - 1)) && (f_string.substring(i + 1, i + 2).match(/[0-9]/))) { result[result.length] = buffer; buffer = ''; } else { if(text == false) { if(buffer.length > 0) { result[result.length] = parseInt(buffer); buffer = ''; } text = true; } buffer += chr; } } if(buffer.length > 0) { if(text) { result[result.length] = buffer; } else { result[result.length] = parseInt(buffer); } } return result; }; var array1 = __strnatcmp_split(f_string1); var array2 = __strnatcmp_split(f_string2); var len = array1.length; var text = true; var result = -1; var r = 0; if(len > array2.length) { len = array2.length; result = 1; } for(i = 0; i < len; i++) { if(isNaN(array1[i])) { if(isNaN(array2[i])){ text = true; if((r = strcmp(array1[i], array2[i])) != 0) { return r; } } else if(text){ return 1; } else { return -1; } } else if(isNaN(array2[i])){ if(text) { return -1; } else{ return 1; } }else { if(text || f_version){ if((r = (array1[i] - array2[i])) != 0){ return r; } } else { if((r = strcmp(array1[i].toString(), array2[i].toString())) != 0) { return r; } } text = false; } } return result; }
To run the Javascript strnatcmp, you will also need the following dependencies:
Examples
Currently there are 5 examples
Example 1
strnatcmp('Price 12.9', 'Price 12.15');And that would return
1Example 2
strnatcmp('Price 12.09', 'Price 12.15');And that would return
-1Example 3
strnatcmp('Price 12.90', 'Price 12.15');And that would return
1More 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 (169.4kB)
- minified: php.min.js (62.1kB) [recommended]
- compressed: php.packed.js (45.4kB)
Namespaced What is 'namespaced?'
- uncompressed source: php.namespaced.js (208.7kB)
- minified: php.namespaced.min.js (62.3kB)
- compressed: php.namespaced.packed.js (45.6kB)
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.
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.
- Site. A place for PHP.JS of it's own. You can track our lame attempts at phpjs.org (not hyperlinked deliberately). If there are any CakePHP developers out there who would like to contribute, contact me.
- Testsuite. A better test-suite that can be ran locally so developers can easily test before commiting. Also the testing itself should be more thorough.
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: 627 times










tagcloud
#2. Kevin (link) on 01 March 2008
#1. Martijn Wieringa on 01 March 2008
I've added this tag to fix the following problem:
When you're comparing numbers in a string.. like:
... [more]
"Price 12.9"
"Price 12.15"
You want: "Price 12.9" > "Price 12.15".
Yet when you use numbers to indicate version numbers.. Like
"Version 12.9"
"Version 12.15"
You want: "Price 12.9" < "Price 12.15".