1

我对这个有点盲目,试图使用 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##

无论我尝试什么,一次我得到of the text,另一次只在哈希之间,有人可以帮助我使用正则表达式吗?

4

3 回答 3

1
$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]);

将输出:

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

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

)
于 2013-01-22T20:40:43.937 回答
1
$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 );

print_r( $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 ) 
于 2013-01-23T08:48:07.713 回答
0

我对正则表达式不太好,但这是我的解决方案,我不匹配以哈希开头的行,^ 用于行开头,(?!#) 是负前瞻寻找# 字符,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);

正如我所说,我是正则表达式的新手,所以如果我错了,专家会警告我。

于 2013-01-22T21:36:03.613 回答