PHP to SEARCH the Upper+Lower Case mixed Words in the strings?

不羁岁月 提交于 2019-12-06 14:25:35

You should be good with:

preg_match_all("/\b([a-z]+[A-Z]+[a-zA-Z]*|[A-Z]+[a-z]+[a-zA-Z]*)\b/", $input, $matches);
print_r($matches[1]); 

Edit: As capturing is not needed, it can be also >>

preg_match_all("/\b(?:[a-z]+[A-Z]+[a-zA-Z]*|[A-Z]+[a-z]+[a-zA-Z]*)\b/", $input, $matches);
print_r($matches[0]); 

Ωmega's answer works just fine. Just for fun, here's an alternate (commented) regex that does the trick using lookahead:

<?php // test.php Rev:20120721_1400
$re = '/# Match word having both upper and lowercase letters.
    \b               # Assert word begins on word boundary.
    (?=[A-Z]*[a-z])  # Assert word has at least one lowercase.
    (?=[a-z]*[A-Z])  # Assert word has at least one uppercase.
    [A-Za-z]+        # Match word with both upper and lowercase.
    \b               # Assert word ends on word boundary.
    /x';

$text ='A quick brOwn FOX called F. 4lviN';

preg_match_all($re, $text, $matches);
$results = $matches[0];
print_r($results);
?>

You could use a simple regular expression, such as:

/\s[A-Z][a-z]+\s/

That could be used like so:

preg_match_all('/\s[A-Z][a-z]+\s/', 'A quick Brown FOX called F. 4lvin', $arr);

Then your $arr variable which has had all the matches added to it, will contain an array of those words:

Array
(
    [0] => Array
    (
        [0] => Brown
    )

)

Edit: Changed the pattern.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!