JaypeeHuda on 2011-09-30 11:26:34
Hello Everyone, 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 !!!!
Brett Zamir on 2011-06-21 18:59:53
@kirk bushell: Please read the second question on our FAQ: https://github.com/kvz/phpjs/wiki/FAQ about use of JavaScript.
kirk bushell on 2011-06-21 09:11:35
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.
ELIANE on 2011-03-16 20:33:11
TENHO MINHA SENHA MAIS NÃO CONSIGO ENTRAR NO MEU HOTMAIL
Kevin van Zonneveld on 2010-09-08 17:48:05
@ Sly: Thanks for helping Chris make sense of this : )
Sly on 2010-08-08 09:55:33
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.
Brett Zamir on 2010-06-02 11:19:03
@Eugene: indexOf does not work in IE arrays, but it does exist on strings in IE...
Eugene on 2010-06-01 16:58:08
indexOf is not working in IE
myltik on 2010-04-15 14:05:31
thanks for script , works fine for me :-)
Brett Zamir on 2010-03-26 06:55:42
@Chris: Try dropping the third argument (or setting it to 0) which sets a starting point. It is working correctly...
Chris on 2010-03-25 16:25:24
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.
Brett Zamir on 2009-04-30 04:03:45
@ 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.
Kevin van Zonneveld on 2009-04-29 15:29:10
@ Khurram Adeeb Noorani: Other people do like that. But that's ok. Just compile your own version without the 'duplicate' functions!
Khurram Adeeb Noorani on 2009-04-29 07:46:45
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()
anonymous on 2009-01-20 02:24:19

your_stuff('ghatiya');

Brett Zamir on 2009-01-20 02:16:36
Argh... Sorry, I meant to say that this would be executed in your example:
if (strpos("ABCand.so.on", "ABC") === 0)
but this would not
if (strpos("ABCand.so.on", "ABC") === false)

Brett Zamir on 2009-01-20 02:14:07
@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:
if (strpos("ABCand.so.on", "ABC"))
because that will indeed not be executed. If you ran into this problem, the solution is to do this in your code:
if (strpos("ABCand.so.on", "ABC") === false)
which will be executed
mb on 2009-01-19 16:37:31
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.
Kevin van Zonneveld on 2008-10-06 12:07:34
@ Onno Marsman: Good job!
Onno Marsman on 2008-10-04 17:25:34
Improved to accept things other than strings like in PHP, and a little optimiztion in the return line ( I used === instead of >=)

function strpos( haystack, needle, offset){
    var i = (haystack+'').indexOf( needle, offset ); 
    return i===-1 ? false : i;
}

Kevin van Zonneveld on 2008-09-13 15:28:44
@ 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.
steve on 2008-09-09 20:19:10
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.

if(someString.indexOf('secretcode')){
  //do your magic
}
if you *need* the offset option, you can accomplish with:

function index_of(haystack, needle, offset){
  if(offset){
    haystack = haystack.substr(offset);
  }
  return haystack.indexOf(needle);
}
Then again, maybe I'm missing the point of this exercise?
Kevin van Zonneveld on 2008-09-08 00:16:22
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.
Kirk Strobeck on 2008-09-06 00:43:42
This is great. Just what I needed. This small conversion basically means you don't need to learn JavaScript if you know PHP. Nice.
oooooooooooo on 2008-08-25 18:47:38
Thank you!