-2

I want to get all uppercase string from a text using regex and PHP, for example in this sentence " Hello FROM USA Tô ENGLAND "

I want to get those words " FROM USA ENGLAND ", which means the word must have uppercase characters (all the characters) not only one.

I try to use this regex but without any solution

preg_match_all('/\b([A-Z]+)\b/', $text, $matches);
sayou
  • 893
  • 8
  • 29

1 Answers1

0

I hope it's what do you need:

$text = 'Hello FROM USA to ENGLAND';
preg_match_all('/\b([A-Z]+)\b/u', $text, $matches);
print(implode(' ', $matches[0]));

Output:

FROM USA ENGLAND

You forgot to use implode function for the get string also you need only first result in $matches.

Dmitry Leiko
  • 3,970
  • 3
  • 25
  • 42
  • When writing an answer, you should also write an explanation what you've changed and why. Specially when the change is only a few characters. – M. Eriksson Oct 13 '20 at 14:21