note 60037 modified in function.strpos by danbrown

From: Date: Sun, 17 May 2009 16:37:37 +0000
Subject: note 60037 modified in function.strpos by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-154866@lists.php.net to get a copy of this message
If you want to find positions of all needle's in haystack, you can use this one: <?php while (($pos=strpos($haystack,$needle,$pos+1))!==false) $pos_array[$i++]=$pos; ?> But mind, that it will find from second char. You must use $pos=-1; before you want search from first char. <?php $haystack="one two three one two three one two three one two three one"; $needle="one"; $pos=-1; while (($pos=strpos($haystack,$needle,$pos+1))!==false) $pos_array[$i++]=$pos; ?> RESULT: $pos_array[0] = 0 $pos_array[1] = 14 $pos_array[2] = 28 $pos_array[3] = 42 $pos_array[4] = 56 --was-- If you want to find positions of all needle's in haystack, you can use this one: while (($pos=strpos($haystack,$needle,$pos+1))!==false) $pos_array[$i++]=$pos; But mind, that it will find from second char. You must use $pos=-1; before you want search from first char. { $haystack="one two three one two three one two three one two three one"; $needle="one"; $pos=-1; while (($pos=strpos($haystack,$needle,$pos+1))!==false) $pos_array[$i++]=$pos; } RESULT: $pos_array[0] = 0 $pos_array[1] = 14 $pos_array[2] = 28 $pos_array[3] = 42 $pos_array[4] = 56 http://php.net/manual/en/function.strpos.php

« previous php.notes (#154866) next »