Find second char occurrence in php string

Imagine I have this line:

$info="portugal,alemanha,belgica,porto 1-0 alemanha, belgica 2-0"; 

I want to know the position of the second char "-", so I need a 2-0 result, not a 1-0 result.

I use this function, but always returns the first position,

 $pos = strpos($info, '-'); 

Any idea? Thanks

+6
source share
3 answers

try it

 preg_match_all('/-/', $info,$matches, PREG_OFFSET_CAPTURE); echo $matches[0][1][1]; 
+7
source

The simplest solution for this particular case is to use the offset parameter:

 $pos = strpos($info, '-', strpos($info, '-') + 1); 

You might want to learn regular expressions .

+14
source

You need to use offset parameter

 $pos = strpos($info, '-', [offset]); 

It will work just fine.

+1
source

All Articles