UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

76 lines (63 loc) 2.19 kB
--- layout: page title: "JavaScript strcspn function" comments: true sharing: true footer: true alias: - /functions/view/strcspn:534 - /functions/view/strcspn - /functions/view/534 - /functions/strcspn:534 - /functions/534 --- <!-- Generated by Rakefile:build --> A JavaScript equivalent of PHP's strcspn {% codeblock strings/strcspn.js lang:js https://raw.github.com/kvz/phpjs/master/functions/strings/strcspn.js raw on github %} function strcspn (str, mask, start, length) { // From: http://phpjs.org/functions // + original by: Brett Zamir (http://brett-zamir.me) // * example 1: strcspn('abcdefg123', '1234567890'); // * returns 1: 7 // * example 2: strcspn('123abc', '1234567890'); // * returns 2: 3 start = start ? start : 0; var count = (length && ((start + length) < str.length)) ? start + length : str.length; strct: for (var i = start, lgth = 0; i < count; i++) { for (var j = 0; j < mask.length; j++) { if (str.charAt(i).indexOf(mask[j]) !== -1) { continue strct; } }++lgth; } return lgth; } {% endcodeblock %} - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/strings/strcspn.js) Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are the closest match to this hashtable-like data structure. Please also note that php.js offers community built functions and goes by the [McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just: - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/strings/strcspn.js) ### Example 1 This code {% codeblock lang:js example %} strcspn('abcdefg123', '1234567890'); {% endcodeblock %} Should return {% codeblock lang:js returns %} 7 {% endcodeblock %} ### Example 2 This code {% codeblock lang:js example %} strcspn('123abc', '1234567890'); {% endcodeblock %} Should return {% codeblock lang:js returns %} 3 {% endcodeblock %} ### Other PHP functions in the strings extension {% render_partial _includes/custom/strings.html %}