Use PHP functions in JavaScript

JavaScript substr_count

Returns the number of times a substring occurs in the string

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
function substr_count (haystack, needle, offset, length) {
    // Returns the number of times a substring occurs in the string  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/substr_count    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // *     example 1: substr_count('Kevin van Zonneveld', 'e');
    // *     returns 1: 3
    // *     example 2: substr_count('Kevin van Zonneveld', 'K', 1);    // *     returns 2: 0
    // *     example 3: substr_count('Kevin van Zonneveld', 'Z', 0, 10);
    // *     returns 3: false
    var pos = 0,
        cnt = 0; 
    haystack += '';
    needle += '';
    if (isNaN(offset)) {
        offset = 0;    }
    if (isNaN(length)) {
        length = 0;
    }
    offset--; 
    while ((offset = haystack.indexOf(needle, offset + 1)) != -1) {
        if (length > 0 && (offset + needle.length) > length) {
            return false;
        } else {            cnt++;
        }
    }
 
    return cnt;}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
substr_count('Kevin van Zonneveld', 'e');

Should return

1
3

» Example 2

Running

1
substr_count('Kevin van Zonneveld', 'K', 1);

Should return

1
 

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 substr_count 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
Rafal
6 Oct '11 Permalink

q  Great!

Świetne, bardzo mi się przydało.
Greetings from Poland - Rafal

Gravatar
Robert Eisele
5 Sep '11 Permalink

q  What about this optimization?

function substr_count(haystack, needle, offset, length) {

    haystack+= "";
    needle+= "";

    if (isNaN(offset)) {
        offset = 0;
    }
    if (isNaN(length)) {
        length = haystack.length - offset;
    }

    haystack = haystack.substr(offset, length);

    return (haystack.length - haystack.replace(new RegExp(needle, 'g'), "").length) / needle.length;
}

Gravatar
Kevin van Zonneveld
24 Jul '09 Permalink

q  @ Shock: I think I would have to make the same point as here:
http://phpjs.org/functions/substr_count:559#comment_626

Don't you agree?

Gravatar
Shock
20 Jul '09 Permalink

q  // Maybe something like that?

function substr_count( haystack, needle, offset, length ) {
    if (isNaN(offset)) {
        offset = 0;
    }
    if (isNaN(length)) {
        length = haystack.length - offset;
    }
    if (length <= 0 || (length + offset) > haystack) {
        return false
    } else {
        return haystack.substr(offset,length).split(needle).length - 1;
    }
}



sorry for doublePost

Gravatar
Shock
20 Jul '09 Permalink

q  // Maybe something like that?
[CODE]function substr_count( haystack, needle, offset, length ) {
if (isNaN(offset)) {
offset = 0;
}
if (isNaN(length)) {
length = haystack.length - offset;
}
if (length <= 0 || (length + offset) > haystack) {
return false
} else {
return haystack.substr(offset,length).split(needle).length - 1;
}
}[/CODE

Gravatar
Kevin van Zonneveld
25 Jan '09 Permalink

q  Well we could benchmark it of course. But I think that when your doing this on really large strings, you also end up with really large arrays, that you really don't need. So my guess is that your approach will be more memory intensive don't you agree?

Gravatar
antimatter15
18 Jan '09 Permalink

q  Wouldn't it be faster to just use split().length-1?

[CODE=&quot;Javascript&quot;]function substr_count( haystack, needle, offset, length ) {
if(!isNaN(offset)){
if(!isNaN(length)){
haystack=haystack.substr(offset,length);
}else haystack = haystack.substr(offset)
}
haystack = haystack.split(needle).length-1
return haystack&lt;0?false:haystack;
}[/CODE]


Contribute a New function