Unable to find position of the first occurrence in a string

I need to find position of the first character occurrence in a string that does not repeat. So far all I have my $pos outputs 0 .

<?php
$sentence = "some long string";
$found = false;

while(!$found && $sentence!== ""){
  $c = $sentence[0];
  $count = substr_count($sentence,$c);

  if ($count == 1) $found = true;
  else $sentence = str_replace($c,"",$sentence);
}
$pos = strpos($sentence, $c);
echo "$c: $pos";

It wil output l:0 . What's up with that?

I understand that $pos = strpos($sentence, $c); will not be the correct way to find position. Why Im confused is, when I echo $c its value is "l". So, I thought if i use its value in strpos it will give me the correct position. So to get help on how to extract this first non repeating character position, i thought I would ask at StackOverlow to point me to the right direction. No need to be d**ks, I just learning and I appreciate the help.


Your code is nearly correct. When I tested it I got 'm: 0', which is indeed the first character only to appear once. The reason the position is 0 is that you have been shrinking the original string every time you tried a new character.

I suggest you copy the string before you start looking for characters in it, and then use the copy to calculate the position at the end, rather than the diminished string.

<?php
$sentence = "some long string";
$sentence_copy = $sentence;
$found = false;

while(!$found && $sentence!== ""){
  $c = $sentence[0];
  $count = substr_count($sentence,$c);
  if ($count == 1) $found = true;
  else $sentence = str_replace($c,"",$sentence);
}

$pos = strpos($sentence_copy, $c);
echo "$c: $pos";

This gives me 'm: 2', which is correct.


Regarding the comment

I need to find position of the first character that does not repeat.

<?php
$numberOfOccurences = array_count_values(str_split($string, 1));
$uniqueCharacters = array_keys(
    array_filter($numberOfOccurences, function($c) { return $c == 1; })
);
echo $uniqueCharacters[0] . ':' . strpos($string, $uniqueCharacters[0]);

The idea is simply, that we treat the string not as a string, but as a set of characters, count every character, then only keep the characters, that appears once ( $c == 1 in array_filter() ) and (assuming, that the functions are stable [1]) take the first one.

[1] "stable" means, that they keep the order. If not you must iteratore over $uniqueCharacters and find the one, that appears first manually.

$positions = array_map(
    $uniqueCharacters,
    functions ($character) use ($string) { return strpos($string, $character); }
);
$pos = min($positions);
echo "{$string[$pos]}:$pos";

http://php.net/manual/en/function.strpos.php

Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.

Returns FALSE if the needle was not found.

secondly, $c = $sentence[0] and strpos($sentence, $c) does not gain you any more information of what you already know.

链接地址: http://www.djcxy.com/p/75360.html

上一篇: 在某个位置之前查找子字符串的第一次出现

下一篇: 无法找到字符串中第一次出现的位置