I have to pick up consecutive capital-letter starting words in a text (using PHP preg_match()
).
So in this text - "this is Some text" it should pick up word "Some", but in this text - "this is Another Piece Of text" it should pick up "Another Piece Of".
I currently have this expression - ([A-Z][a-z]+)+
, but it only picks up every single capital case word. I need them in as a whole line (e.g - [0] => "Another Piece Of"
, but I currently get [0] => "Another", [1] => "Piece", [2] => "Of"
)
How should I update it so that it does what I need?
You can use this:
if (preg_match('~[A-Z][a-z]*(?> [A-Z][a-z]*)*~', $text, $m)) {
echo $m[0];
}
(?> [A-Z][a-z]*)*
represents optional other words.
To be more flexible you can change it to (?>\s+[A-Z][a-z]*)*
Note: if you need to deal with accented words, you can use the \p{Ll}
and \p{Lu}
character classes:
if (preg_match('~\p{Lu}\p{Ll}*(?>\s+\p{Lu}\p{Ll}*)*~', $text, $m)) {
echo $m[0];
}