Use PHP functions in JavaScript

JavaScript strspn

Finds length of initial segment consisting entirely of characters found in mask. If start or/and length is provided works like strspn(substr($s,$start,$len),$good_chars)

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
function strspn (str1, str2, start, lgth) {
    // Finds length of initial segment consisting entirely of characters found in mask. If start or/and length is provided works like strspn(substr($s,$start,$len),$good_chars)  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/strspn    // +   original by: Valentina De Rosa
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: strspn('42 is the answer, what is the question ...', '1234567890');
    // *     returns 1: 2
    // *     example 2: strspn('foo', 'o', 1, 2);    // *     returns 2: 2
    var found;
    var stri;
    var strj;
    var j = 0;    var i = 0;
 
    start = start ? (start < 0 ? (str1.length + start) : start) : 0;
    lgth = lgth ? ((lgth < 0) ? (str1.length + lgth - start) : lgth) : str1.length - start;
    str1 = str1.substr(start, lgth); 
    for (i = 0; i < str1.length; i++) {
        found = 0;
        stri = str1.substring(i, i + 1);
        for (j = 0; j <= str2.length; j++) {            strj = str2.substring(j, j + 1);
            if (stri == strj) {
                found = 1;
                break;
            }        }
        if (found != 1) {
            return i;
        }
    } 
    return i;
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
strspn('42 is the answer, what is the question ...', '1234567890');

Should return

1
2

» Example 2

Running

1
strspn('foo', 'o', 1, 2);

Should return

1
2

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have strspn goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Orme
9 Jan '11 Permalink

q  Hmm, I used such string as str2 -- '\0 \t\x0B' and it works fine without escaping. RegExp get already escaped string with exact characters you expect. One thing that matching should be multiline, so \m modifier should be added.

Gravatar
Brett Zamir
8 Jan '11 Permalink

q  @Orme: Also, in seeing now that str2 is added within a character class in your implementation, you wouldn't need to escape as many characters--only \, ], and - should I think be enough for JS character classes, but you do need to escape them.

Gravatar
Brett Zamir
8 Jan '11 Permalink

q  @Orme: You'll need to escape str2 (e.g., by using preg_quote: http://phpjs.org/functions/preg_quote:491 ) since the RegExp could be problematic if special characters are added without escaping. Could you check whether the performance is still better after such escaping is done and then if it passes some test cases, we could accept it.

Gravatar
Orme
7 Jan '11 Permalink

q  I understand that it's not in format, but using regexp in this function is faster in all browsers.

function strspn (str1, str2, start, lgth) {
	str1 = start ? lgth ? str1.substr(start, lgth) : str1.substr(start) : str1;
	var match = str1.match(new RegExp('^['+str2+']+'));
	return match && match[0] ? match[0].length : 0;
}

Gravatar
Kevin van Zonneveld
30 Dec '08 Permalink

q  @ Brett Zamir: Good job man, added!

Gravatar
Brett Zamir
18 Dec '08 Permalink

q  I think this should add the other two args...

[CODE=&quot;Javascript&quot;]

strspn('42 is the answer, what is the question ...', '1234567890');
strspn(&quot;foo&quot;, &quot;o&quot;, 1, 2); // 2

function strspn(str1, str2, start, lgth){
// http://kevin.vanzonneveld.net
// + original by: Valentina De Rosa
// % note 1: Good start, but still missing the 3rd &amp; 4th argument which came to PHP in version 4.3.0
// * example 1: strspn('42 is the answer, what is the question ...', '1234567890');
// * returns 1: 2

var found;
var stri;
var strj;
var j = 0;
var i = 0;

start = start ? (start &lt; 0 ? (str1.length+start) : start) : 0;
lgth = lgth ? ((lgth &lt; 0) ? (str1.length+lgth-start) : lgth) : str1.length-start;
str1 = str1.substr(start, lgth);

for(i = 0; i &lt; str1.length; i++){
found = 0;
stri = str1.substring(i,i+1);
for (j = 0; j &lt;= str2.length; j++) {
strj = str2.substring(j,j+1);
if (stri == strj) {
found = 1;
break;
}
}
if (found != 1) {
return i;
}
}

return i;
}[/CODE]


Contribute a New function