UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

66 lines (54 loc) 1.99 kB
--- layout: page title: "JavaScript strrchr function" comments: true sharing: true footer: true alias: - /functions/view/strrchr:546 - /functions/view/strrchr - /functions/view/546 - /functions/strrchr:546 - /functions/546 --- <!-- Generated by Rakefile:build --> A JavaScript equivalent of PHP's strrchr {% codeblock strings/strrchr.js lang:js https://raw.github.com/kvz/phpjs/master/functions/strings/strrchr.js raw on github %} function strrchr (haystack, needle) { // From: http://phpjs.org/functions // + original by: Brett Zamir (http://brett-zamir.me) // + input by: Jason Wong (http://carrot.org/) // + bugfixed by: Brett Zamir (http://brett-zamir.me) // * example 1: strrchr("Line 1\nLine 2\nLine 3", 10).substr(1) // * returns 1: 'Line 3' var pos = 0; if (typeof needle !== 'string') { needle = String.fromCharCode(parseInt(needle, 10)); } needle = needle.charAt(0); pos = haystack.lastIndexOf(needle); if (pos === -1) { return false; } return haystack.substr(pos); } {% endcodeblock %} - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/strings/strrchr.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/strrchr.js) ### Example 1 This code {% codeblock lang:js example %} strrchr("Line 1\nLine 2\nLine 3", 10).substr(1) {% endcodeblock %} Should return {% codeblock lang:js returns %} 'Line 3' {% endcodeblock %} ### Other PHP functions in the strings extension {% render_partial _includes/custom/strings.html %}