phpjs
Version:
247 lines (190 loc) • 6.04 kB
HTML
<!-- Generated by Rakefile:build -->
<strong>
Ghabriel Nunes
</strong>
on 2012-02-12 20:51:47 <br />
I found some errors in the function above.
The above function does not handle negative limits, and, according to the PHP documentation, if limit is 0, it's treated as 1. The above function treats limit as "not set" if it's 0, which is wrong. Also, if the limit is bigger than the split length, it adds white spaces to the array, which is also wrong. I re-created this function, fixing all the mistakes I found:
Note: sorry for any english mistakes, english is not my first language.
<pre><code>
function explode( delimiter, string, limit ){
if ( arguments.length < 2 || typeof delimiter == 'undefined' || typeof string == 'undefined' ) return null;
if ( delimiter === '' || delimiter === false || delimiter === null) return false;
if ( typeof delimiter == 'function' || typeof delimiter == 'object' || typeof string == 'function' || typeof string == 'object'){
return { 0: '' };
}
if ( delimiter === true ) delimiter = '1';
// Here we go...
delimiter += '';
string += '';
var s = string.split( delimiter );
if ( typeof limit === 'undefined' ) return s;
// Support for limit
if ( limit === 0 ) limit = 1;
// Positive limit
if ( limit > 0 ){
if ( limit >= s.length ) return s;
return s.slice( 0, limit - 1 ).concat( [ s.slice( limit - 1 ).join( delimiter ) ] );
}
// Negative limit
if ( -limit >= s.length ) return [];
s.splice( s.length + limit );
return s;
}
</code></pre>
<hr />
<strong>
<a href="www.prova.com" rel="nofollow">raj </a>
</strong>
on 2011-03-07 06:52:47 <br />
ghghggh
<hr />
<strong>
a
</strong>
on 2010-06-14 16:08:09 <br />
ser
<hr />
<strong>
<a href="www.webmehr.com" rel="nofollow">IVI-R3za.M</a>
</strong>
on 2010-02-27 07:35:43 <br />
its so great! thanks
<hr />
<strong>
<a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a>
</strong>
on 2010-02-15 07:00:28 <br />
It's working for me...
<pre><code>explode('-+-', 'abc-+-def-+-ghi') // abc,def,ghi</code></pre>
What are you trying to do that's different?
<hr />
<strong>
nowotny
</strong>
on 2010-02-14 17:05:13 <br />
PHP explode function allows you to split by a string, not only one character, which this JS function does not allow...
<hr />
<strong>
Onno Marsman
</strong>
on 2008-12-09 18:25:49 <br />
@valulgi: You'll need to look a bit closer. This function is really built around the split function from js.
<hr />
<strong>
<a href="www.valugi.ro" rel="nofollow">valulgi</a>
</strong>
on 2008-12-08 13:49:34 <br />
why not use the split function from js?
<hr />
<strong>
<a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
</strong>
on 2008-05-17 15:16:24 <br />
@ d3x: Thanks! I've updated the function. I changed the
<pre><code>
arguments.length != 3
</code></pre>
part, for it to allow 2 arguments. Other than that, great contribution!!
<hr />
<strong>
d3x
</strong>
on 2008-05-17 09:40:35 <br />
Same function with the limit attribute:
<pre><code>
function explode( delimiter, string, limit ) {
// http://kevin.vanzonneveld.net
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: kenneth
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: explode(' ', 'Kevin van Zonneveld');
// * returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
// + further improved by: d3x
// * example 1: explode('=', 'a=bc=d', 2);
// * returns : ['a', 'bc=d']
var emptyArray = { 0: '' };
if ( arguments.length != 3
|| typeof arguments[0] == 'undefined'
|| typeof arguments[1] == 'undefined'
|| typeof arguments[2] == 'undefined' )
{
return null;
}
if ( delimiter === ''
|| delimiter === false
|| delimiter === null )
{
return false;
}
if ( typeof delimiter == 'function'
|| typeof delimiter == 'object'
|| typeof string == 'function'
|| typeof string == 'object' )
{
return emptyArray;
}
if ( delimiter === true ) {
delimiter = '1';
}
if(!limit){
return string.toString().split ( delimiter.toString() );
} else {
var splitted = string.toString().split(delimiter.toString());
var partA = splitted.splice(0, limit - 1);
var partB = splitted.join(delimiter.toString());
partA.push(partB);
return partA;
}
}
</code></pre>
<hr />
<strong>
u24
</strong>
on 2008-02-06 11:00:20 <br />
great project.
this one doesn't replicate the optional third $limit parameter which php has though. If I get a spare second I'll add another comment with amended code.
<hr />
<strong>
<a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
</strong>
on 2008-02-01 07:57:52 <br />
@ kenneth: That was pretty ugly indeed, thanks for contributing, I've updated the source and added your name.
<hr />
<strong>
kenneth
</strong>
on 2008-02-01 06:40:58 <br />
This function is wrong. Params are reversed, and return values are not what they would be in PHP.
The &quot;description&quot; above is actually correct, but both the actual source code given and the example given have the params reversed.
As for return values....I might as well just right it out at this point:
<pre><code>function explode( /* delimiter, string */ ) {
var emptyArray = { 0: '' };
if ( arguments.length != 2
|| typeof arguments[0] == 'undefined'
|| typeof arguments[1] == 'undefined' )
{
return null;
}
var delimiter = arguments[0];
var string = arguments[1];
if ( delimiter === ''
|| delimiter === false
|| delimiter === null )
{
return false;
}
if ( typeof delimiter == 'function'
|| typeof delimiter == 'object'
|| typeof string == 'function'
|| typeof string == 'object' )
{
return emptyArray;
}
if ( delimiter === true ) {
delimiter = '1';
}
return string.toString().split ( delimiter.toString() );
}</code></pre>
<hr />