Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.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 此正则表达式为true,但在localhost中不起作用_Php_Regex_Localhost - Fatal编程技术网

Php 此正则表达式为true,但在localhost中不起作用

Php 此正则表达式为true,但在localhost中不起作用,php,regex,localhost,Php,Regex,Localhost,我有一个正则表达式,我用 但它在localhost中不起作用!(我的xampp版本为7.0.6) 我的代码是下面的代码 html.html {block content ddggggggggggggggg /endcontent} file.php $pt="~\{\s*block\s*-?\s*(\w+)[\s+|\~](.*)\/end\1}~s"; #Blocks# preg_match($pt, file_get_contents('html.html'),$match1); prin

我有一个正则表达式,我用

但它在localhost中不起作用!(我的xampp版本为7.0.6) 我的代码是下面的代码

html.html

{block content
ddggggggggggggggg
/endcontent}
file.php

$pt="~\{\s*block\s*-?\s*(\w+)[\s+|\~](.*)\/end\1}~s";
#Blocks#
preg_match($pt, file_get_contents('html.html'),$match1);
print_r($match1);exit;
我猜问题是\1,因为下面的代码工作正常

$pt="~\{\s*block\s*-?\s*(\w+)[\s+|\~](.*)\/endcontent}~s";
#Blocks#
preg_match($pt, self::$tmp,$match1);
print_r($match1);exit;
为什么第一个代码在我的本地主机中不起作用? 你知道有什么问题吗


html.html文件不是静态的,可能不同。我需要一个动态正则表达式,例如first regex

您需要使用一个带引号的文本,以使
\1
被视为反向引用(否则,您需要双重转义它)

此外,要匹配1+空格或
~
,需要使用带括号的分组,而不是字符类。请注意,
[\s+\~]
匹配一个字符:空格、
+
~
,我怀疑您是否真的想要这种行为

使用


请参见

在正则表达式周围使用单引号。我还认为,如果要匹配1+空格或
~
,则在
\s+\124;\~
周围需要一个
()
。请尝试
$pt='~\{\s*块\s*-?\s*(\w+)(\s+)(.*)\/end\1}~s'投票。。。完美的请张贴你的答案
$s = "{block content\nddggggggggggggggg\n/endcontent}";
$pt='~\{\s*block\s*-?\s*(\w+)(\s+|\~)(.*)\/end\1}~s';
preg_match($pt, $s, $match1);
print_r($match1);