Reputation: 57
I'm trying to use a preg_replace or similar php function to: - identify the first all capital letter word in a string, - and insert a character directly after it (a dash or semi-colon will do) - the all capital letter word should be 3 characters long or more.
So far I have the regular expression:
/(?<!\ )([^A-Z{3,}])/
But, this isn't working in terms of only words that are 3+ characters. I'm also not sure I have it 'strictly' only looking at the very first word.
I believe that once I have the regex sorted out - this
$string = "LONDON On November 12th twelve people...";
$replaced_string = preg_replace('/myregex/',': ', $string);
will output as the following
LONDON: On November 12th twelve people..."
Upvotes: 2
Views: 2889
Reputation: 76395
It's a fairly simple regex, really:
$replacedString = preg_replace('/\b([A-Z]{3,})\b/', '$1: ', $string);
It works like this:
\b
: word boundary. This detects the start and end of a "word"([A-Z]{3,})
: Match 3 or more upper-case characters. The brackets capture this part of the match, so we can use it in the replacement string\b
: Another word boundaryReplace this match with:
'$1: '
: the $1
refers back to the first captured group (the 3 or more upper case characters). To this, we're adding a colon and a space. That will be our replacement stringThis will add the colon and space after all upper-case words of 3 or more characters. To replace only 1 word, just pass a limit to preg_replace
:
$replaced = preg_replace('/\b([A-Z]{3,})\b/', '$1: ', $string, 1);
Where that last argument is the number of matches you wish to replace. -1 for all, 1 for 1, 2 for 2, etc...
Judging by your sample string, the upper-case words are city names. It's possible for city names to contain a dash, or even a space. To address this, you might want to match all strings containing upper-case chars, dashes and spaces:
$replaceAll = preg_replace('/\b([A-Z -]{2,}[A-Z])\b/', '$1: ', $string);
What changed:
([A-Z -]{2,}
: The capturing match start with upper-case chars (2 or more, not 3), but also matches spaces and dashes.[A-Z])
: The last character of the captured group must be an upper-case character, this avoids capturing the trailing spaces or dashes. The result is that we capture stuff like "NEW YORK" or "FOO-TOWN", but not "ON - Something".The rest is the same as before. If you want to allow for other characters that might occur (like a dot) just add them to the first part of the capturing group. The most complete pattern will probably be something like this:
$replaced = preg_replace('/\b([A-Z][A-Z .-]+[A-Z])\b/', '$1: ', $string);
This ensures the captured group starts, and ends with an upper case character, and contains any number of upper-case chars, spaces, dots and dashes in between. So this will match something like "ST. LEWIS", too
Upvotes: 5