JavaScript strpos
Finds position of first occurrence of a string within another
1 2 3 4 56 7 8 9 1011 12 13 14 | function strpos (haystack, needle, offset) { // Finds position of first occurrence of a string within another // // version: 1109.2015 // discuss at: http://phpjs.org/functions/strpos // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + improved by: Onno Marsman // + bugfixed by: Daniel Esteban // + improved by: Brett Zamir (http://brett-zamir.me) // * example 1: strpos('Kevin van Zonneveld', 'e', 5); // * returns 1: 14 var i = (haystack + '').indexOf(needle, (offset || 0)); return i === -1 ? false : i; } |
Examples
Running
1 | strpos('Kevin van Zonneveld', 'e', 5); |
Should return
1 | 14 |
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 strpos goodness in JavaScript.
@kirk bushell: Please read the second question on our FAQ: https://github.com/kvz/phpjs/wiki/FAQ about use of JavaScript.
I don't understand the need for a library like this - why not simply learn the language and learn the functions that are available rather than holding onto a language that you love? By learning you become a better developer, rather than writing wrappers for problems that don't exist.
In the example there is a "5":
strpos('Kevin van Zonneveld', 'e', 5);
If you see, it means, fnc strpos will search for 'e' after the 5th position of haystack string. Try the example with 0. ;) Like this:
strpos('Kevin van Zonneveld', 'e', 0);
>>>>>I really don't understand why you think this works - even your example isn't correct. Find the first occurence of 'e' in 'Kevin van Zonneveld', and you think the answer should be 14? Hmm what about the 'e' in 'Kevin', the first word, and the second letter??
Try to find 'v' and it will come back with 6, missing the 'v' in 'Kevin' once more.
@Chris: Try dropping the third argument (or setting it to 0) which sets a starting point. It is working correctly...
I really don't understand why you think this works - even your example isn't correct. Find the first occurence of 'e' in 'Kevin van Zonneveld', and you think the answer should be 14? Hmm what about the 'e' in 'Kevin', the first word, and the second letter??
Try to find 'v' and it will come back with 6, missing the 'v' in 'Kevin' once more.
@ Khurram Adeeb Noorani: Also note that the PHP functions have some extra functionality (e.g,. str_replace() can be used with arrays) or extra arguments (like strpos's offset). Besides, for those only familiar with PHP, they can fall back on the PHP names as need be.
@ Khurram Adeeb Noorani: Other people do like that. But that's ok. Just compile your own version without the 'duplicate' functions!
Hi,
Thanks for the wonderful functions, they helped me a lot ... Bu what I think is that we need only those functions that are not available in javascript, there is no need of strpos(), explode(), substr() etc, as they are already available in javascript as indexOf() and split() and substr()
Argh... Sorry, I meant to say that this would be executed in your example:
[CODE="Javascript"]if (strpos("ABCand.so.on", "ABC") === 0)[/CODE]
but this would not
[CODE="Javascript"]if (strpos("ABCand.so.on", "ABC") === false)[/CODE]
@mb : It does work with your example. You just have to keep in mind that because the index position (array indexes start at 0) in your case is '0', it will return 0, so you cannot use it as a boolean like:
[CODE="Javascript"]if (strpos("ABCand.so.on", "ABC"))[/CODE]
because that will indeed not be executed.
If you ran into this problem, the solution is to do this in your code:
[CODE="Javascript"]if (strpos("ABCand.so.on", "ABC") === false)[/CODE]
which will be executed
I've noticed that this function does not find for example "ABC" in "ABCand.so.on".
Bug: if first char of needle is first char of haystack, this does not work.
Improved to accept things other than strings like in PHP, and a little optimiztion in the return line ( I used === instead of >=)
[CODE="Javascript"]
function strpos( haystack, needle, offset){
var i = (haystack+'').indexOf( needle, offset );
return i===-1 ? false : i;
}
[/CODE]
@ steve: Well it would be nice if you can copy-paste generic PHP code into a .JS file, and it still works. Straying from the PHP path, means developers have yet another variable to keep track of.. Does this function behave differently from the original PHP function, just because PHP.JS developers think they're smarter?
So we've chosen to stick with PHP and duplicate all of it's features.. and even flaws.
ok, here's where I get real lost.
I get the point about trying to port the functions - fine.
but strpos()?
this function in PHP has a HORRIBLE bug where it sometimes returns an int, and sometimes a boolean, and worse yet if the string you want is at the zero index, what do you test for?
In JavaScript, all string objects have a .indexOf() and a .lastIndexOf() function that work (IMHO) better than the PHP one, in fact I've seen many libs mimic the JS behavior in a PHP version of index_of()
JS .indexOf()
returns the (zero based index as an integer (if found))
else
returns the integer -1 (e.g. not found)
This works much better as you can have logic like.
[CODE="Javascript"]
if(someString.indexOf('secretcode')){
//do your magic
}
[/CODE]
if you *need* the offset option, you can accomplish with:
[CODE="Javascript"]
function index_of(haystack, needle, offset){
if(offset){
haystack = haystack.substr(offset);
}
return haystack.indexOf(needle);
}
[/CODE]
Then again, maybe I'm missing the point of this exercise?
I would still learn JS if I was you.. Don't miss out on beautiful things like jQuery. PHP.JS is just a tool to smoothen up the edges between client <> server interaction development.


JaypeeHuda
30 Sep '11
The strpos() function is used to search for a character/text within a string. i.e. with the help of strpos() function you can search a specific character or specific text string. If match is found, strops() function will returns character position of first match. If no match is found, it will return false (Nothing to display).............................. for more details please check out following link...
http://mindstick.com/Articles/4550476c-822d-4506-b41f-edf5ec8228a7/?PHP%20String%20function
thanks !!!!