如何从字符串中查找(和替换)大写字母的单词


How to find (and replace) words with capital letters from a string?

$string = "WORD is the first HEJOU is the Second BOOM is the Third";
$sring = str_replce('???', '???<br>', $string);
echo $string; // <br>WORD is the first <br>HEJOU is the Second <br>BOOM is the Third

插图说明了一切。我想选择所有大写字母的单词(不是以大写字母开头的单词),并在它前面替换一些东西。什么好主意吗?

$string = "WORD is the first HEJOU is the Second BOOM is the Third";
$string = preg_replace("#'b([A-Z]+)'b#", "<br>''1", $string);
echo $string;

文稿
<br>WORD is the first <br>HEJOU is the Second <br>BOOM is the Third

使用的正则表达式表示:

'b - Match a word boundary, zero width
[A-Z]+ - Match any combination of capital letters 
'b - Match another word boundary
([A-Z]+) - Capture the word for use in the replacement

然后,在替换

''1, replace with the captured group.

str_replace只是将特定字符串替换为其他特定字符串。您可以使用preg_replace

print preg_replace('~'b[A-Z]+'b~','<br>''0',$string);

使用正则表达式

http://php.net/manual/en/function.preg-replace.php