strpos - how to deal with numbers and separators?

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
tiago
Posts: 589
Joined: 14 Feb 2011 21:41

strpos - how to deal with numbers and separators?

Post 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').
Power-hungry user!!!

highend
Posts: 14955
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

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

Post by highend »

which is false anyway
No, this is false...

2 is correct, because the first token is referenced by 0, not 1.
One of my scripts helped you out? Please donate via Paypal

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

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

Post by tiago »

"false for the situation I need to deal with", not the returned value...
Power-hungry user!!!

highend
Posts: 14955
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

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

Post by highend »

and the situation you have to deal with is?

Just add +1 to the return value and you're good to go?
One of my scripts helped you out? Please donate via Paypal

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

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

Post 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'...
Power-hungry user!!!

highend
Posts: 14955
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

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

Post 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...
One of my scripts helped you out? Please donate via Paypal

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

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

Post 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.
Power-hungry user!!!

PeterH
Posts: 2829
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

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

Post 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?

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

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

Post 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
Power-hungry user!!!

PeterH
Posts: 2829
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

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

Post 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:

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

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

Post 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...
Power-hungry user!!!

Post Reply