fork download
  1. <?php
  2. $str = "International Scrabble Assocation";
  3. preg_match_all('/\b(\w)\w*\b/i', $str, $matches);
  4. $regex = '/\b' . implode('\S*\s*', array_map(strtoupper, $matches[1])) . '\S*\b/';
  5. $tests = array('Intl. Scrabble Assoc.',
  6. 'Intl Scrabble Assoc',
  7. 'I.S.A',
  8. 'ISA',
  9. 'Intl. SA',
  10. 'intl scrabble assoc',
  11. 'i.s.a.',
  12. 'isa',
  13. 'lisa',
  14. 'LISA',
  15. 'LI. S. A.');
  16. echo "The generated regex is $regex.\n\n";
  17. foreach ($tests as $test)
  18. {
  19. echo "Does '$test' match? " . (preg_match($regex, $test) ? 'Yes' : 'No') . ".\n";
  20. }
  21. ?>
Success #stdin #stdout 0.02s 13112KB
stdin
Standard input is empty
stdout
The generated regex is /\bI\S*\s*S\S*\s*A\S*\b/.

Does 'Intl. Scrabble Assoc.' match? Yes.
Does 'Intl Scrabble Assoc' match? Yes.
Does 'I.S.A' match? Yes.
Does 'ISA' match? Yes.
Does 'Intl. SA' match? Yes.
Does 'intl scrabble assoc' match? No.
Does 'i.s.a.' match? No.
Does 'isa' match? No.
Does 'lisa' match? No.
Does 'LISA' match? No.
Does 'LI. S. A.' match? No.