Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php preg_replace从一个单词中分别获取两个字符_Php_Regex_Preg Replace - Fatal编程技术网

Php preg_replace从一个单词中分别获取两个字符

Php preg_replace从一个单词中分别获取两个字符,php,regex,preg-replace,Php,Regex,Preg Replace,实际上,我正试图通过使用preg_replace函数从“Get Word”中提取一个类似“GEWO”的字符串。问题是也可以有一个字符串,比如“Get”,或者“Get Word和另一个单词”,所以我必须分别得到正确的字符串 谢谢 使用以下命令: $s = "Get Word And another Word"; $s = preg_replace('/(?<=\w{2})\w*\b(\s|$)/','',$s); $s = strtoupper($s); echo $s; $s=“获取单词

实际上,我正试图通过使用preg_replace函数从“Get Word”中提取一个类似“GEWO”的字符串。问题是也可以有一个字符串,比如“Get”,或者“Get Word和另一个单词”,所以我必须分别得到正确的字符串

谢谢

使用以下命令:

$s = "Get Word And another Word";
$s = preg_replace('/(?<=\w{2})\w*\b(\s|$)/','',$s);
$s = strtoupper($s);
echo $s;
$s=“获取单词和另一个单词”;

$s=preg_replace('/(?您可以在不使用正则表达式的情况下执行此操作,如下所示:

$str = 'Get Word';
$arr = explode(" ", $str);
foreach($arr as $word){
    $newstr .= strtoupper(substr($word, 0, 2));
}
echo $newstr;
//=> GEWO

“适当的字符串分别是什么?”“Get”变成“GE”,“Get Word and other Word”变成“GEWOANANWO”你的意思是要将字符串分解为单词,从每个单词中获取前两个字符(如果字符串包含1个字母的单词,如
a
I
),会发生什么情况把这些字母大写…你有什么困难?没有只包含一个字符的单词,所以我只需要根据模式分解一个字符串。这正是我需要的。谢谢!