phpjs
Version:
210 lines (168 loc) • 6.24 kB
HTML
<!-- Generated by Rakefile:build -->
<strong>
<a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
</strong>
on 2010-09-08 19:46:46 <br />
@ Bjorn Roesbeke & Rafał Kukawski: Thanks for reporting & fixing the issue. Will be online shortly, but already fixed here: https://github.com/kvz/phpjs/commit/c83cf0d3bd5734444e5397af42b5f560240a9c54
<hr />
<strong>
<a href="http://blog.kukawski.pl" rel="nofollow">Rafa? Kukawski</a>
</strong>
on 2010-09-05 01:00:30 <br />
split_length should be optional
<pre><code>function str_split(string, split_length) {
if(split_length == null){
split_length = 1;
}
if (string == null || split_length < 1) {
return false;
}
string += '';
var chunks = [], pos = 0, len = string.length;
while(pos < len){
chunks.push(string.slice(pos, pos += split_length));
}
return chunks;
}</code></pre>
<hr />
<strong>
<a href="http://blog.kukawski.pl" rel="nofollow">Rafa? Kukawski</a>
</strong>
on 2010-09-05 00:54:17 <br />
The problem is, that IE adds some properties to the result array (input, index, lastIndex) and count() also counts them.
Beside that, str_split won't work for multiline strings.
Below another approach to this problem
<pre><code>function str_split(string, split_length) {
if (string == null || split_length < 1) {
return false;
}
string += '';
var chunks = [], pos = 0, len = string.length;
while(pos < len){
chunks.push(string.slice(pos, pos += split_length));
}
return chunks;
}</code></pre>
<hr />
<strong>
<a href="http://www.bjornroesbeke.be" rel="nofollow">Bjorn Roesbeke</a>
</strong>
on 2010-09-04 23:28:07 <br />
There's something wrong with this function but due to a lack of Javascript knowledge i can only provide the testcase.
I split a string:
http://shared.bjornroesbeke.be/phpjs/count_splitted_test.php
And when i count the number of items the results differ:
http://shared.bjornroesbeke.be/phpjs/count_splitted_result.png
Note that count() seems to function properly:
http://shared.bjornroesbeke.be/phpjs/count_regular_test.php
<hr />
<strong>
<a href="http://jabe.ir" rel="nofollow">nimatramon</a>
</strong>
on 2010-03-31 03:00:28 <br />
thanx man
<hr />
<strong>
<a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
</strong>
on 2009-10-25 13:58:56 <br />
@ Theriault: As always, brilliant stuff man! I've added your code. Proof:
http://github.com/kvz/phpjs/commit/718895323939ff5e0f674a537e9a4546fce71c6e ; )
Will be online here shortly as well!
<hr />
<strong>
Theriault
</strong>
on 2009-10-18 05:04:48 <br />
Here's a shorter version that runs faster than the current one.
<pre><code>
function str_split(s, l) {
if (s == undefined || !s.toString || l < 1) {
return false;
}
return s.toString().match(new RegExp('.{1,' + (l || '1') + '}', 'g'));
}
</code></pre>
<hr />
<strong>
<a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
</strong>
on 2009-02-15 16:35:08 <br />
@ tomasoma: You could also look at the explode() function. It's based on JavaScript's own .split() function, but adds support for PHP's arguments: limit.
Maybe I'm not getting you right though: Feel free to point out something I'm missing here.
<hr />
<strong>
<a href="http://bahai-library.com" rel="nofollow">Brett Zamir</a>
</strong>
on 2009-02-15 01:58:43 <br />
Hello and bonjour tomasoma,
Thanks for sharing. Kevin likes to stick with the default PHP behavior in functions here, but I presume he won't mind a useful tip here.
However, I'm not clear though why you felt you needed to add a split function which JavaScript already has? Is it fixing bug in some browser? In Firefox I get the same behavior with your function as with JS split()...
<hr />
<strong>
<a href="www.tomatoma.eu" rel="nofollow">tomasoma</a>
</strong>
on 2009-02-14 18:11:29 <br />
I'm a novice sorry my comments are in french hehehe
I just needed the "real" split function which return a table from a string divide by a specified character : here is a new method for the string object :
good web site great ideas !!! thanks !!! ;-)
<pre><code>
// le split sur un caractère qui retourne un tableau
// bug peut-être si le caractère recherché est présent à l'index 0
function split(car){
var tab = new Array();
var deb;
var fin=0;
var i=0;
var test;
while(test!=-1){
deb=fin;
fin=this.indexOf(car,deb+1);
test=fin;
if(fin==-1){fin=this.length;}
if(deb!=0){deb++;}
tab[i]=this.substring(deb,fin);
i++;
}
// alert(tab);
return tab;
}
String.prototype.split = split;
function test(){
var text="toma%soma%c'est cool on test tout%76898644";
alert(text.split("%"));
}
</code></pre>
<hr />
<strong>
<a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
</strong>
on 2008-05-02 11:46:44 <br />
@ Brett Zamir: I agree we should stick to PHP's implementation wherever we can. Thanks for your improvement!
<hr />
<strong>
Brett Zamir
</strong>
on 2008-04-30 16:32:31 <br />
While the &quot;f_backwards&quot; argument might not do any harm, I prefer not to add distinctions not present in PHP, if for nothing else than if PHP changes later (of course it can be ignored here for now, but...)
However, one additional change in my own version below which I added and think yours should to is to allow a default of 1 for length (as in PHP):
<pre><code>function str_split ( f_string, f_split_length){
// http://kevin.vanzonneveld.net
// + original by: Martijn Wieringa
// * example 1: str_split('Hello Friend', 3);
// * returns 1: ['Hel', 'lo ', 'Fri', 'end']
if (f_split_length == undefined) {
f_split_length = 1;
}
if(f_split_length &gt; 0){
var result = [];
while(f_string.length &gt; f_split_length) {
result[result.length] = f_string.substring(0, f_split_length);
f_string = f_string.substring(f_split_length);
}
result[result.length] = f_string;
return result;
}
return false;
}</code></pre>
<hr />