Search code examples
phpstrpos

How to find numbers that only appears once in a string?


Let's say I have input string

String 1 : 1234123
String 2 : 76523752

From those string, I'd like to know which numbers that only appears once. So in my case the output should be

String 1 : array(4)
String 2 : array(6,3)

I'm guessing I'll use strpos() for this task but I couldn't get any logical flow to make this happen. Can you help me figure this out?


Solution

  • Boring solution but it works!

    $string="1234123";
    $result=array();
    foreach (count_chars($string, 1) as $i => $val) {
         if($val==1) $result[]=chr($i);
    }
    print_r($result);