Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.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_匹配在字符串开头查找整个单词?_Php_String_Preg Match - Fatal编程技术网

Php 用preg_匹配在字符串开头查找整个单词?

Php 用preg_匹配在字符串开头查找整个单词?,php,string,preg-match,Php,String,Preg Match,我试图在preg_match字符串的开头找到完整的单词,例如“Location:”、“Contact:” 请有人提供一个简单的例子。以单词字符(\w)开始(^),并有一个或多个(+),应该捕获并存储在$matches(括号)中 以单词字符(\w)开始(^),并具有一个或多个字符(+),应捕获并存储在$matches(括号)中 $in = "First word is captured"; preg_match("/^(\w+)/",$in,$matches); echo $matches[1];

我试图在preg_match字符串的开头找到完整的单词,例如“Location:”、“Contact:”

请有人提供一个简单的例子。

以单词字符(
\w
)开始(
^
),并有一个或多个(
+
),应该捕获并存储在$matches(括号)中

以单词字符(
\w
)开始(
^
),并具有一个或多个字符(
+
),应捕获并存储在$matches(括号)中

$in = "First word is captured";
preg_match("/^(\w+)/",$in,$matches);
echo $matches[1];