fork download
  1. <?php
  2.  
  3. // your code goes here
  4.  
  5. $initial_string = "abab sbs abc ffuuu qwerty uii onnl ghj";
  6.  
  7. // Remove words
  8. $result_string = preg_replace('/\b(?=\w*(\w)\w*\1)\w+/', '', $initial_string);
  9.  
  10. // Clean spacing, because the string currently looks like ' abc qwerty ghj'
  11. $result_string = trim(preg_replace('/\s+/', ' ', $result_string));
  12.  
  13. print_r("Initial String:\n$initial_string\nBecomes:\n$result_string\nMAGIC!\n\n");
  14.  
  15. // Or, another way
  16.  
  17. // Extract all non-repeating character words.
  18. preg_match_all('/\b(?!\w*(\w)\w*\1)\w+/', $initial_string, $matches);
  19.  
  20. // Glue them all together with a space in-between
  21. $result_string = implode(' ', $matches[0]);
  22.  
  23. print_r("Initial String:\n$initial_string\nBecomes:\n$result_string\nMAGIC!\n\n");
  24.  
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
Initial String:
abab sbs abc ffuuu qwerty uii onnl ghj
Becomes:
abc qwerty ghj
MAGIC!

Initial String:
abab sbs abc ffuuu qwerty uii onnl ghj
Becomes:
abc qwerty ghj
MAGIC!