识别并获取PHP字符串中的大写单词 [英] Identify and get capitalized words in a string in php

查看:325
本文介绍了识别并获取PHP字符串中的大写单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在句子或字符串上是否有获取CAPS单词的功能或方法? 示例:$str = "ABOUT US who we are" 我如何识别并获取大写的单词"ABOUT US"并将其分隔为小写字母的单词"who we are"?这样输出将是$str1 = "ABOUT US"$str2 = "who we are" ...谢谢!

Is there a function or way on how to get the CAPS words on a sentence or string? Example: $str = "ABOUT US who we are" how am i able to identify and get the capitalized words "ABOUT US"and separate it to small letter words "who we are"? So that the output would be $str1 = "ABOUT US" and $str2 = "who we are"... thanks!

推荐答案

您可以为其使用正则表达式:

You can use a regular expression for it:

preg_match_all('/\b([A-Z]+)\b/', $str, $matches);

要匹配小写字母,请使用以下命令:

To match the lowercase words, use this:

preg_match_all('/\b([a-z]+)\b/', $str, $matches2);

然后,您可以使用implode()之类的功能来构建字符串.

Then you can use functions such as implode() to build the strings.

这篇关于识别并获取PHP字符串中的大写单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆