获取字符串中第一个字符的重复值
Get the repeated values of the first character in a string
我有一个和这个类似的字符串,
$string = "01110111011101110111101110111110111111";
我需要获取字符串中的第一个字符(在本例中为 0)。然后我需要获取该字符在字符串中所有出现的位置。所有出现的都放在一个数组中,第一个元素是1(第一次出现的是字符串的第一个字符)。
例如,上面的字符串应该产生这样的数组,
$return_value = array([0]=>1, [1]=>5, [2]=>9, [3]=>13, [4]=> 17....);
这应该可以解决问题,
$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);
哪个return_value会产生,
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)}
以上是我的回答,希望对你有所帮助
$string = "01110111011101110111101110111110111111";
$return_value = array();
$select = 0; //Select value you want to find
for($i = 0; $i < strlen($string); $i++)
{
if($string[$i] == $select) $return_value[] = $i + 1; //Get the position in string
}
var_dump($return_value);
Result
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) }
一个简单的解决方案:将字符串拆分成字符然后运行遍历列表并在当前字符发生变化时注册。
当它从0
变为1
时是一个开始:记住当前位置;当它从1
变为0
时就是结束:记住之前的位置(最后一个1
的位置)。
$string = "01110111011101110111101110111110111111";
// The current character
// Used also as index in $positions (0 => starts, 1 => ends)
// And also used to adjust the stored position:
// ends are 1 character less that the current position
$current = '0';
// Collect starts and ends here ([0] => starts, [1] => ends)
$positions = array(array(), array());
// The current position in string; start before the first character
$pos = -1;
// Split the string to characters
foreach (str_split($string) as $digit) {
// Advance to the current position in string
$pos ++;
// When the current character changes...
if ($digit != $current) {
// ... put the position in the appropriate list
$positions[$current][] = $pos - $current;
}
// Update current character
$current = $digit;
}
// If the last digit was a '1' then it is an (unrecorded) end
if ($current == '1') {
// There is no need to adjust, $pos is strlen($string)-1
$positions[$current][] = $pos;
}
// Dump the result
echo("start: ". implode(', ', $positions[0])."\n");
echo("end : ". implode(', ', $positions[1])."\n");
我有一个和这个类似的字符串,
$string = "01110111011101110111101110111110111111";
我需要获取字符串中的第一个字符(在本例中为 0)。然后我需要获取该字符在字符串中所有出现的位置。所有出现的都放在一个数组中,第一个元素是1(第一次出现的是字符串的第一个字符)。
例如,上面的字符串应该产生这样的数组,
$return_value = array([0]=>1, [1]=>5, [2]=>9, [3]=>13, [4]=> 17....);
这应该可以解决问题,
$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);
哪个return_value会产生,
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)}
以上是我的回答,希望对你有所帮助
$string = "01110111011101110111101110111110111111";
$return_value = array();
$select = 0; //Select value you want to find
for($i = 0; $i < strlen($string); $i++)
{
if($string[$i] == $select) $return_value[] = $i + 1; //Get the position in string
}
var_dump($return_value);
Result
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) }
一个简单的解决方案:将字符串拆分成字符然后运行遍历列表并在当前字符发生变化时注册。
当它从0
变为1
时是一个开始:记住当前位置;当它从1
变为0
时就是结束:记住之前的位置(最后一个1
的位置)。
$string = "01110111011101110111101110111110111111";
// The current character
// Used also as index in $positions (0 => starts, 1 => ends)
// And also used to adjust the stored position:
// ends are 1 character less that the current position
$current = '0';
// Collect starts and ends here ([0] => starts, [1] => ends)
$positions = array(array(), array());
// The current position in string; start before the first character
$pos = -1;
// Split the string to characters
foreach (str_split($string) as $digit) {
// Advance to the current position in string
$pos ++;
// When the current character changes...
if ($digit != $current) {
// ... put the position in the appropriate list
$positions[$current][] = $pos - $current;
}
// Update current character
$current = $digit;
}
// If the last digit was a '1' then it is an (unrecorded) end
if ($current == '1') {
// There is no need to adjust, $pos is strlen($string)-1
$positions[$current][] = $pos;
}
// Dump the result
echo("start: ". implode(', ', $positions[0])."\n");
echo("end : ". implode(', ', $positions[1])."\n");