Use PHP functions in JavaScript

JavaScript strrpos

Finds position of last occurrence of a string within another 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
function strrpos (haystack, needle, offset) {
    // Finds position of last occurrence of a string within another string  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/strrpos    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   input by: saulius
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: strrpos('Kevin van Zonneveld', 'e');    // *     returns 1: 16
    // *     example 2: strrpos('somepage.com', '.', false);
    // *     returns 2: 8
    // *     example 3: strrpos('baa', 'a', 3);
    // *     returns 3: false    // *     example 4: strrpos('baa', 'a', 2);
    // *     returns 4: 2
    var i = -1;
    if (offset) {
        i = (haystack + '').slice(offset).lastIndexOf(needle); // strrpos' offset indicates starting point of range till end,        // while lastIndexOf's optional 2nd argument indicates ending point of range from the beginning
        if (i !== -1) {
            i += offset;
        }
    } else {        i = (haystack + '').lastIndexOf(needle);
    }
    return i >= 0 ? i : false;
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

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

Should return

1
16

» Example 2

Running

1
strrpos('somepage.com', '.', false);

Should return

1
8

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 strrpos 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
Brett Zamir
8 Jan '10 Permalink

q  @saulius, thanks again, I've now incorporated your fix and the issue I mentioned: http://github.com/kvz/phpjs/raw/master/functions/strings/strrpos.js . The problem is that the 2nd argument to lastIndexOf() works very differently from strrpos's offset argument (see the comments for an explanation).

Gravatar
Brett Zamir
7 Jan '10 Permalink

q  I don't have time to test myself now, but there is also this issue:

var_dump(strrpos("canal", "a", 3)); // PHP and php.js gives 3
var_dump(strrpos("canal", "a", 4)); // PHP gives false while php.js gives 3



Seems the offset behaves differently, maybe from the opposite direction. So before adding saulius' changes, someone please take a look.

Gravatar
saulius
6 Jan '10 Permalink

q  var i = (haystack+'').lastIndexOf( needle, offset );
return i >= 0 ? i : false;
//strrpos(somepage.com, '.', false); returns false


if (offset)
{
var i = (haystack+'').lastIndexOf(needle, offset);
}
else
{
var i = (haystack+'').lastIndexOf(needle);
}

Gravatar
Kevin van Zonneveld
30 Jan '08 Permalink

q  @ Abraham Estrada: Yep, but sometimes you need a different set of wheels right?

@ speedmax:
About the PHP vs JS stuff.. I'm not trying to port or emulate the entire language or control structures of PHP. Indeed I don't see the need because Javascript seems to have more elegant features in that category anyway.

However in my eyes, PHP does provide a large set of standard functions that make developing very easy, and some of them don't have good standard Javascript implementations, though they often would be great to have client-side.

So in this project by also providing the functions separately, I hope to keep people from inventing the wheel and give them a head start.

Gravatar
Abraham Estrada
30 Jan '08 Permalink

q  Sorry guys but I think you are reinventing the wheel

Gravatar
speedmax
30 Jan '08 Permalink

q  that was showing lamda in the work, here is one liner to the purist.

Beauty and the Beast
[CODE="Javascript"]
['you','and','me'].map(String.toUpperCase).join(' ')
[/CODE]

[CODE="PHP"]
implode(' ', array_map('strtolower', array('you', 'and', 'me')))
[/CODE]

Gravatar
speedmax
30 Jan '08 Permalink

q  I been coding php for 7 years, good work but why the hell would you want to do that.. ?


javascript is a better language, its more of a functional language with array/hash shortcut, iterator, closure for free.

try to do this in php

['you','and','me'].map(function(item){
return item.toUpperCase()
}).join(' ')


Contribute a New function