哪个php函数可用于从字符串中提取大写单词?
例如,我有:
... a large text that contains CAPITALIZED PARTS ...
我需要以编程方式从中提取CAPITALIZED PARTS
。
我搜索了互联网,但没有找到任何解决方案。
答案 0 :(得分:1)
您可以使用\\b[A-Z]+\\b
正则表达式获取带有preg_match_all的ALLCAPS字词:
<?php
$re = "/\\b[A-Z]+\\b/";
$str = "a large text that contains CAPITALIZED PARTS";
preg_match_all($re, $str, $matches);
foreach ($matches as $val) {
print_r($val);
}
?>
输出:
Array
(
[0] => CAPITALIZED
[1] => PARTS
)
答案 1 :(得分:1)
preg_match_all('/\b([A-Z]+)\b/', $str, $matches);
答案 2 :(得分:1)
我会使用默认区分大小写的正则表达式
$string='... a large text that contains CAPITALIZED PARTS ...'
preg_match_all('%[A-Z]+%', $string, $matches);
var_dump($matches);
答案 3 :(得分:0)
$re = "/[A-Z]*/";
$str = "a large text that contains CAPITALIZED PARTS";
preg_match_all($re, $str, $matches);
$newArr = array();
foreach($matches[0] as $mat){
if(strlen($mat)>0)
$newArr[] = $mat;
}
print_r($newArr);