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').
strpos - how to deal with numbers and separators?
-
tiago
- Posts: 589
- Joined: 14 Feb 2011 21:41
strpos - how to deal with numbers and separators?
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?
No, this is false...which is false anyway
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?
"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?
and the situation you have to deal with is?
Just add +1 to the return value and you're good to go?
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?
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'...
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?
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;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?
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.
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?
Add a separator, i.e. "," to start and end of haystack and of needle, and try!
Does this help?
Code: Select all
echo strpos(",1,111,11,", ",11,", , , ",");-
tiago
- Posts: 589
- Joined: 14 Feb 2011 21:41
Re: strpos - how to deal with numbers and separators?
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
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?
It seems you missed a "," at the end of the 1st parm?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
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
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
-
tiago
- Posts: 589
- Joined: 14 Feb 2011 21:41
Re: strpos - how to deal with numbers and separators?
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...
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!!!
XYplorer Beta Club