Skip to content

Instantly share code, notes, and snippets.

@kadko
Last active April 18, 2019 21:52
Show Gist options
  • Save kadko/4b77123d79167b6e5e5d35b02e0a0d38 to your computer and use it in GitHub Desktop.
Save kadko/4b77123d79167b6e5e5d35b02e0a0d38 to your computer and use it in GitHub Desktop.
//Finds nth occourance of string after position of given needle. //Pass $needle argument as empty string [''] if you want to find from start //Pass $needle argument as Int to search string from this position
//Finds nth occourance of string after position of given needle.
//Pass $needle argument as empty string [''] if you want to find from start
//Pass $needle argument as Int to search string from this position
function substr_Index( $string, $needle, $delimiter, $nth ){
$str2 = '';
$posf = strpos($string, $needle);
if($posf !== false){
$string = substr($string, $posf);
$posTotal = $posf;
}else{
$posTotal = 0;
}
if( is_int($needle) ){
$posTotal = $needle;
}
for($i=0; $i < $nth; $i++){
if($str2 != ''){
$string = $str2;
}
$pos = strpos($string, $delimiter);
$str2 = substr($string, $pos + 1);
$posTotal += $pos+1;
}
return $posTotal-1;
}
//example (returns part of given string from second [:] next from first [#] )
$str_ = '..:....:..:....#,61185:9789756130346:0000000:369615:97860510:61436=0000000323636';
echo substr($str_, substr_Index( $str_, '#' , ':', 2) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment