php - Get the repeated values of the first character in a string -


i have strings similar one,

$string = "01110111011101110111101110111110111111"; 

i need first character in string (in case, 0). need positions of occurrences of character in string. occurrences should put array first element being 1 (the first occurrence first character of string).

for example, above string should produce array this,

$return_value = array([0]=>1, [1]=>5, [2]=>9, [3]=>13, [4]=> 17....); 

this should trick,

$string = "01110111011101110111101110111110111111"; $offset = 0; $return_value = array(); $character = substr($string, 0, 1);  while (($offset = strpos($string, $character, $offset))!== false) {     $return_value[] = $offset + 1;     $offset = $offset + strlen($character); }  var_dump($return_value); 

which return_value produce,

array(8) { [0]=> int(1) [1]=> int(5) [2]=> int(9) [3]=> int(13) [4]=> int(17) [5]=> int(22) [6]=> int(26) [7]=> int(32)} 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -