简体   繁体   English

如何从PHP中的字符串获取大写字母序列?

[英]How to get upper case sequences from string in PHP?

In PHP , how do I get only upper case sequences from the string? PHP中 ,如何从字符串中仅获取大写字母序列?

For example, how can I get from: 例如,如何获得:

abcDEF GHi jklm

only: 只要:

DEF GH

To find matches: 查找匹配项:

preg_match('/[A-Z]+/', $input, $matches);

To remove all "wrong" stuff: 删除所有“错误”的东西:

$output = preg_replace('/[^A-Z ]/', '', $input);

使用此正则表达式[AZ]+

A string of upper case letters can be matched with [AZ]+ 一串大写字母可以与[AZ]+匹配

However, if you specifically want words that are only upper case, then you need to add a word boundary marker to each end of the expression. 但是,如果您特别希望只使用大写字母的单词,则需要在表达式的每一端添加单词边界标记。 This is \\b . 这是\\b

So your expression would look like this: 因此,您的表情将如下所示:

/\b[A-Z]+\b/

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM