Page 1 of 1

strpos - how to deal with numbers and separators?

Posted: 22 Aug 2012 18:23
by tiago
hi guys.
is there a way i could do this?

echo strpos("1,111,11", "11", , , ","); // 3 - reported by separator

currently it reports "2", which is false anyway (ok, true in the context of current implementation, but false for the situation I need to deal with, ie, reporting the occurrence of the absolute "11" and not the first '11').

Re: strpos - how to deal with numbers and separators?

Posted: 22 Aug 2012 18:50
by highend
which is false anyway
No, this is false...

2 is correct, because the first token is referenced by 0, not 1.

Re: strpos - how to deal with numbers and separators?

Posted: 22 Aug 2012 18:57
by tiago
"false for the situation I need to deal with", not the returned value...

Re: strpos - how to deal with numbers and separators?

Posted: 22 Aug 2012 19:09
by highend
and the situation you have to deal with is?

Just add +1 to the return value and you're good to go?

Re: strpos - how to deal with numbers and separators?

Posted: 22 Aug 2012 19:19
by tiago
No, I need strpos to return the position (first occurrence) of a exact value assuming there's a separator between them.

If the exact value isn't there, then report "-1".

echo strpos("1,111,11111", "11", , , ","); // -1
-currently reports '2'...

Re: strpos - how to deal with numbers and separators?

Posted: 22 Aug 2012 19:48
by highend

Code: Select all

$search = "11111";
	$a = "1,111,11111,1111111";
	$i = 1;
	foreach($number, $a, ",") {
		$token = gettoken($a, $i, ",");
		if($token == $search) {
			$found = $i;
			break;
		} else {
			$found = -1;
		}
		$i++;
	}
	msg "Position is: " . $found;
Not very elegant but does it's job...

Re: strpos - how to deal with numbers and separators?

Posted: 22 Aug 2012 19:56
by tiago
Yes, I did something like that which turned out to be very cpu intensive.
Strpos is astonishingly faster and that's why I asked for such implementation.

Re: strpos - how to deal with numbers and separators?

Posted: 22 Aug 2012 22:11
by PeterH
Add a separator, i.e. "," to start and end of haystack and of needle, and try!

Code: Select all

   echo strpos(",1,111,11,", ",11,", , , ",");
Does this help?

Re: strpos - how to deal with numbers and separators?

Posted: 24 Aug 2012 01:00
by tiago
Because
echo strpos(",1,111,11111,11", ",11,", , , ","); // -1

-why not a simple solution for this? It does make sense, it's already implemented in other incarnations of commands, and anything else is making an easy task harder. sigh

Re: strpos - how to deal with numbers and separators?

Posted: 24 Aug 2012 10:03
by PeterH
tiago wrote:Because
echo strpos(",1,111,11111,11", ",11,", , , ","); // -1

-why not a simple solution for this? It does make sense, it's already implemented in other incarnations of commands, and anything else is making an easy task harder. sigh
It seems you missed a "," at the end of the 1st parm?

In a form usable inside a program:

Code: Select all

 $var = "1,111,11111,11";
 $num = "11";   // this number might be unquoted
 $sep = ",";

 echo strpos($sep.$var.$sep, $sep.$num.$sep, , , $sep);   // 12
The returned number might be corrected by -1 :whistle:

Edit: maybe some explanation:

StrPos() relates to strings. What you are looking for would be something like "TokenPos()"...

But if you are looking for a (better: for the first) matching delimited token in a string you can basically search for that token by adding the token delimiters, i.e. ",11," for token "11" - so I added the 2 delimiters for the second operand in my code.

Sure this wouldn't function with the first or last token - both miss one delimiter. You can help by adding these delimiters to the first parameter. Now every token is inside delimiters.

Advantage: avoid a loop, simple code.

I was wrong with the hint of correcting the result by -1 :arrow: the search string is prefixed by a delimiter, (i.e. 1 char is added,) but the match is on the leading search-delimiter, not on the first char of the search token, so 1 char is subtracted again :roll:

Re: strpos - how to deal with numbers and separators?

Posted: 31 Aug 2012 19:33
by tiago
Yes, you got it.

Now and then I face problems like this so a TokenPos would be a more than ideal solution.

Just posted another related question such TokenPos may cover.

Let's pray...