Suppose you have this string:
假设你有这个字符串:
hiThere
hiThere
What is the fastest way to find where the first upper case character is? (T
in this example)
查找第一个大写字符所在位置的最快方法是什么?在这个例子中(T)
I am worried about performance as some words are quite long.
我担心性能,因为有些词很长。
9 个解决方案
#1
14
Easiest would be to use preg_match (if there is only 1 match) or preg_match_all (if you want all the matches) http://php.net/manual/en/function.preg-match.php
最简单的方法是使用preg_match(如果只有一个匹配)或preg_match_all(如果需要所有匹配)http://php.net/manual/en/function.preg-match.php
preg_match_all('/[A-Z]/', $str, $matches, PREG_OFFSET_CAPTURE);
Not sure if it is the fastest..
不知道是不是最快的。
#2
6
In order to find the first uppercase character, I would use the PREG_OFFSET_CAPTURE
flag of preg_match
:
为了找到第一个大写字符,我将使用PREG_OFFSET_CAPTURE标志:
$string = "hiThere";
preg_match( '/[A-Z]/', $string, $matches, PREG_OFFSET_CAPTURE );
print_r( $matches[0] );
Which returns the following:
这将返回以下:
Array ( [0] => T [1] => 2 )
You could wrap this logic up into a function and use it over and over:
你可以把这个逻辑打包成一个函数,反复使用:
function firstUC ( $subject ) {
$n = preg_match( '/[A-Z]/', $subject, $matches, PREG_OFFSET_CAPTURE );
return $n ? $matches[0] : false;
}
echo ( $res = firstUC( "upperCase" ) ) ? $res[1] : "Not found" ;
// Returns: 5
echo ( $res = firstUC( "nouppers!" ) ) ? $res[1] : "Not found" ;
// Returns: Not found
#3
5
Other way of doing
其他的方法
$stringCamelCase = 'stringCamelCase';// hiThere
Way with preg_split():
与preg_split():
$array = preg_split('#([A-Z][^A-Z]*)#', $stringCamelCase, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
echo strlen($array[0]);// $array = ['string', 'Camel', 'Case']
Way with strpbrk():
与strpbrk():
$CamelCase = strpbrk($stringCamelCase, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
echo strpos($stringCamelCase, $CamelCase);
#4
2
I'd imagine doing this:
我想像这样做:
strlen(preg_replace("/^([a-z]*)[A-Z].*$/", "$1", $str));
isThis
=> 2
largerWord
=> 6
isThis => 2 largerWord => 6
#5
1
Use strpos()
.
使用大小写敏感()。
echo strpos($string, "T");
#6
1
Had the darndest time with this function till I found the array is a multi-dimensional. Didn't see anyone's code mention this. To get the position of the first Capitalized letter I used :
在我发现数组是多维的之前,我对这个函数做了最大胆的尝试。没有看到任何人的代码提到这一点。为了得到我使用的第一个大写字母的位置:
<?php
$field = "i_select_Interior_Quote";
preg_match( '/[A-Z]/', $field, $m, PREG_OFFSET_CAPTURE );
$posi=$m[0][1];
echo '<br><br>'.$posi; // outputs 9
?>
#7
0
Solution for a-z characters. Probably wont work unicode
解决方案所有字符。unicode可能不会工作
preg_match('/[A-Z]/', $str, $m);
if(isset($m[1])) {
echo 'First upper char is '.$m[1].' and located at '. strpos($string, "T");
}
else {
'There is no upperchar';
}
#8
0
Here's a (more or less) one line solution.
这是一个(或多或少)一行的解决方案。
$pos = strcspn($string, 'ABCDEFGHJIJKLMNOPQRSTUVWXYZ');
strcspn will return the position of the first occurrence of any character in the second argument, or the length of the string if no character occurs.
strcspn将返回第二个参数中第一个出现字符的位置,或者如果没有出现字符,则返回字符串的长度。
Returns the length of the initial segment of str1 which does not contain any of the characters in str2.
返回str1的初始段长度,该段不包含str2中的任何字符。
$pos = strcspn($string, 'ABCDEFGHJIJKLMNOPQRSTUVWXYZ');
if ($pos < strlen($string)) {
echo "capital letter as index $pos";
}
#9
0
What I have used in my own application:
我在自己的申请中使用的:
function acronyms($s){
$s=str_replace(array('& ','of ','the ','and '),'',strtolower($s));
$s=ucwords(trim($s)); //Collecting data
if(strlen($s)>0 && preg_match_all('/[A-Z]/',$s,$m)) return implode('',$m[0]);
return $s;
}
#1
14
Easiest would be to use preg_match (if there is only 1 match) or preg_match_all (if you want all the matches) http://php.net/manual/en/function.preg-match.php
最简单的方法是使用preg_match(如果只有一个匹配)或preg_match_all(如果需要所有匹配)http://php.net/manual/en/function.preg-match.php
preg_match_all('/[A-Z]/', $str, $matches, PREG_OFFSET_CAPTURE);
Not sure if it is the fastest..
不知道是不是最快的。
#2
6
In order to find the first uppercase character, I would use the PREG_OFFSET_CAPTURE
flag of preg_match
:
为了找到第一个大写字符,我将使用PREG_OFFSET_CAPTURE标志:
$string = "hiThere";
preg_match( '/[A-Z]/', $string, $matches, PREG_OFFSET_CAPTURE );
print_r( $matches[0] );
Which returns the following:
这将返回以下:
Array ( [0] => T [1] => 2 )
You could wrap this logic up into a function and use it over and over:
你可以把这个逻辑打包成一个函数,反复使用:
function firstUC ( $subject ) {
$n = preg_match( '/[A-Z]/', $subject, $matches, PREG_OFFSET_CAPTURE );
return $n ? $matches[0] : false;
}
echo ( $res = firstUC( "upperCase" ) ) ? $res[1] : "Not found" ;
// Returns: 5
echo ( $res = firstUC( "nouppers!" ) ) ? $res[1] : "Not found" ;
// Returns: Not found
#3
5
Other way of doing
其他的方法
$stringCamelCase = 'stringCamelCase';// hiThere
Way with preg_split():
与preg_split():
$array = preg_split('#([A-Z][^A-Z]*)#', $stringCamelCase, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
echo strlen($array[0]);// $array = ['string', 'Camel', 'Case']
Way with strpbrk():
与strpbrk():
$CamelCase = strpbrk($stringCamelCase, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
echo strpos($stringCamelCase, $CamelCase);
#4
2
I'd imagine doing this:
我想像这样做:
strlen(preg_replace("/^([a-z]*)[A-Z].*$/", "$1", $str));
isThis
=> 2
largerWord
=> 6
isThis => 2 largerWord => 6
#5
1
Use strpos()
.
使用大小写敏感()。
echo strpos($string, "T");
#6
1
Had the darndest time with this function till I found the array is a multi-dimensional. Didn't see anyone's code mention this. To get the position of the first Capitalized letter I used :
在我发现数组是多维的之前,我对这个函数做了最大胆的尝试。没有看到任何人的代码提到这一点。为了得到我使用的第一个大写字母的位置:
<?php
$field = "i_select_Interior_Quote";
preg_match( '/[A-Z]/', $field, $m, PREG_OFFSET_CAPTURE );
$posi=$m[0][1];
echo '<br><br>'.$posi; // outputs 9
?>
#7
0
Solution for a-z characters. Probably wont work unicode
解决方案所有字符。unicode可能不会工作
preg_match('/[A-Z]/', $str, $m);
if(isset($m[1])) {
echo 'First upper char is '.$m[1].' and located at '. strpos($string, "T");
}
else {
'There is no upperchar';
}
#8
0
Here's a (more or less) one line solution.
这是一个(或多或少)一行的解决方案。
$pos = strcspn($string, 'ABCDEFGHJIJKLMNOPQRSTUVWXYZ');
strcspn will return the position of the first occurrence of any character in the second argument, or the length of the string if no character occurs.
strcspn将返回第二个参数中第一个出现字符的位置,或者如果没有出现字符,则返回字符串的长度。
Returns the length of the initial segment of str1 which does not contain any of the characters in str2.
返回str1的初始段长度,该段不包含str2中的任何字符。
$pos = strcspn($string, 'ABCDEFGHJIJKLMNOPQRSTUVWXYZ');
if ($pos < strlen($string)) {
echo "capital letter as index $pos";
}
#9
0
What I have used in my own application:
我在自己的申请中使用的:
function acronyms($s){
$s=str_replace(array('& ','of ','the ','and '),'',strtolower($s));
$s=ucwords(trim($s)); //Collecting data
if(strlen($s)>0 && preg_match_all('/[A-Z]/',$s,$m)) return implode('',$m[0]);
return $s;
}