Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.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_match_all_Php_Regex - Fatal编程技术网

Php 正则表达式查找并替换为preg_match_all

Php 正则表达式查找并替换为preg_match_all,php,regex,Php,Regex,我在这一点上有点盲目,试图使用preg_match_all来查找散列之间的文本 ##START of the text## the text <b>starts</b> here etc ##END of the the text## ##START of the text 2## Other version stringtext <b>starts</b> here etc ##END of the the text 2## ##文本开头##

我在这一点上有点盲目,试图使用preg_match_all来查找散列之间的文本

##START of the text##
the text <b>starts</b> here etc
##END of the the text##
##START of the text 2##
Other version stringtext <b>starts</b> here etc
##END of the the text 2##
##文本开头##
文本从这里开始等等
##文末##
##正文的开头2##
其他版本的stringtext从这里开始等
##正文2结束##
不管我怎么做,一次我得到文本的
,另一次只有在散列之间,有人能帮我处理正则表达式吗?

$string='##文本的开头##
$string = '##START of the text##
the text <b>starts</b> here etc
##END of the the text##
##START of the text 2##
Other version stringtext <b>starts</b> here etc
##END of the the text 2##';

preg_match_all('/##START.+?##[\n](.+?)##END/s', $string, $matches);
// look for any characters between ##START and ##END, 's' will make it match newlines as well

print_r($matches[1]);
文本从这里开始等等 ##文末## ##正文的开头2## 其他版本的stringtext从这里开始等 ##课文结束2###'; preg#u match#u all('/##START.+?##[\n](.+?)##END/s',$string,$matches); //查找##START和###END之间的任何字符,“s”也将使其与换行符匹配 打印($matches[1]);
将输出:

Array
(
    [0] => the text <b>starts</b> here etc

    [1] => Other version stringtext <b>starts</b> here etc

)
数组
(
[0]=>文本从这里开始,等等
[1] =>其他版本的stringtext从此处开始等
)

我对正则表达式不太在行,但这是我的解决方案,我不匹配以散列开头的行,^表示行开始,(?!#)表示负前瞻,查找#字符,m修饰符表示多行

$string = <<<EOD
##START of the text##
the text <b>starts</b> here etc
##END of the the text##
##START of the text 2##
Other version stringtext <b>starts</b> here etc
##END of the the text 2##
EOD;

preg_match_all('~^(?!#).*~m',$string,$match);
echo "<pre>";
print_r($match);

$string=那么你是想捕获文本的
开头还是
文本从这里开始等
?请你发布你的一些尝试…@nickb试图捕获
文本从这里开始等
其他版本的strinttext…
@Lix我已经尝试过f.e.
预匹配所有(/(#开始[.*.+.],$txt,$matches)
thnx,对我帮助很大,但是我如何才能只获得
文本从这里开始等
其他版本的stringtext从这里开始等
$str = '##START of the text##
the text <b>starts</b> here etc
##END of the the text##
##START of the text 2##
Other version stringtext <b>starts</b> here etc
##END of the the text 2##';

preg_match_all( '~##(.+)##~', $str, $matches );
Array ( [0] => START of the text [1] => END of the the text [2] => START of the text 2 [3] => END of the the text 2 )