Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.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 正则表达式预匹配字_Php_Regex_Preg Match - Fatal编程技术网

Php 正则表达式预匹配字

Php 正则表达式预匹配字,php,regex,preg-match,Php,Regex,Preg Match,我试图获取字符串的一部分,该部分以例如Name:开头。如果整个字符串看起来像Name:Carl,我只想要Carl部分,而不是Name:前缀 我该怎么做?我试过: $data = file_get_contents('page.html'); $regex = '/Name:.*/'; preg_match($regex,$data,$match); var_dump($match); 但我得到的结果是: 数组(1){[0]=>字符串(28)“名称:Carl” 另一件事我不明白的是为什么会显示数

我试图获取字符串的一部分,该部分以例如
Name:
开头。如果整个字符串看起来像
Name:Carl
,我只想要
Carl
部分,而不是
Name:
前缀

我该怎么做?我试过:

$data = file_get_contents('page.html');
$regex = '/Name:.*/';
preg_match($regex,$data,$match);
var_dump($match);
但我得到的结果是:

数组(1){[0]=>字符串(28)“名称:Carl”


另一件事我不明白的是为什么会显示
数组(1){[0]=>字符串(28)

对于您的匹配线,请执行以下操作:

$regex = '/Name:(.*)/'; 

匹配的部分(在(.*)中)将以$match形式出现。

您必须将要检索的内容放在()中:


我投了更高的票,因为在一般情况下包含I是好的,但应该强调的是,这将使匹配不区分大小写——很可能这不是OP想要的。
'/Name:(.*)/i'