Use PHP functions in JavaScript

JavaScript strpbrk

Search a string for any of a set of characters

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
function strpbrk (haystack, char_list) {
    // Search a string for any of a set of characters  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/strpbrk    // +   original by: Alfonso Jimenez (http://www.alfonsojimenez.com)
    // +   bugfixed by: Onno Marsman
    // +    revised by: Christoph
    // +    improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: strpbrk('This is a Simple text.', 'is');    // *     returns 1: 'is is a Simple text.'
    for (var i = 0, len = haystack.length; i < len; ++i) {
        if (char_list.indexOf(haystack.charAt(i)) >= 0)
            return haystack.slice(i);
    }    return false;
}
external links: original PHP docs | raw js source

Examples

Running

1
strpbrk('This is a Simple text.', 'is');

Should return

1
'is is a Simple text.'

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 strpbrk 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
Kevin van Zonneveld
24 Jul '09 Permalink

q  @ Christoph: Cool man, much shorter, no IE problems & passes testcases. So I'm happy. Thanks for the code! (will be online shortly)

Gravatar
Christoph
21 Jul '09 Permalink

q  Doesn't work in IE as IE can't acces characters by array subscription - you'll have to use charAt() instead! Also, it replicates built-in functionality.

A better implementation:

function strpbrk(string, chars) {
    for(var i = 0, len = string.length; i < len; ++i) {
        if(chars.indexOf(string.charAt(i)) >= 0)
                return string.substring(i);
    }

    return false;
}


Contribute a New function