php 如何在PHP中检测字符串是否包含1个大写字母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4426327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:02:50  来源:igfitidea点击:

How to detect if string contains 1 uppercase letter in PHP

phpregexstring

提问by Bob Cavezza

Couldn't find a function for this. I'm assuming I need to use regex?

找不到此功能。我假设我需要使用正则表达式?

I'm trying to do html redirects in php in cases where the url contains at least 1 upper case letter.

在 url 包含至少 1 个大写字母的情况下,我正在尝试在 php 中进行 html 重定向。

example: http://www.domain.com/Michael_Jordanneeds to be redirected to http://www.domain.com/michael_jordan- only problem is I can't seem to find a script to detect if at least 1 capital letter exists.

例如:http: //www.domain.com/Michael_Jordan需要重定向到http://www.domain.com/michael_jordan- 唯一的问题是我似乎无法找到一个脚本来检测是否至少有 1 个大写字母存在。

回答by RageZ

Some regular expression should be able to the work, you can use preg_matchand [A-Z]

一些正则表达式应该可以工作,你可以使用preg_match[A-Z]

if(preg_match('/[A-Z]/', $domain)){
 // There is one upper
}

回答by Tyler Eaves

if (strtolower($url) != $url){
  //etc...

回答by Ateszki

You can also try this

你也可以试试这个

if (!ctype_lower($string)) {
    // there is at least une uppercase character
}

not sure if this is more efficient than the other two methods proposed.

不确定这是否比提出的其他两种方法更有效。

回答by inf3rno

preg_match_all('%\p{Lu}%usD', 'aA,éá,eE,éé,iI,íí,oO,óó,??,??,uU,úú,üü,??', $m);
echo '<pre>';
var_dump($m);
echo '</pre>';

Tested with hungarian utf-8 characters, [A-Z] is for latin1 only.

使用匈牙利语 utf-8 字符进行测试,[AZ] 仅适用于 latin1。

回答by johnbessa

Here is a simpler eg:

这是一个更简单的例子:

$mydir = "C:\Users\John" ;

print preg_match('/^[A-Z]:\.*/', $mydir, $match )."\n" ;
print $match[0]. " preg match \n" ;

Produces:

产生:

1
C: preg match

This suggests that the parens are not necessary --for one match, at least

这表明括号不是必需的——至少对于一场比赛

Look at this to be more specific for your application: PHP to SEARCH the Upper+Lower Case mixed Words in the strings?

看看这个更适合您的应用程序:PHP to SEARCH the Upper+Lower Case Mixed Words in the strings?