我有一个html文件和两个关键字,我想得到这两个关键字内驻留的所有文本。我应该使用正则表达式吗?我想把这两个关键词作为输入。如果你能举个例子就好了。
是的,使用Regex: keyword1(.*?)keyword2
。PHP的例子:
preg_match_all('/'.$kwdOne.'(.*?)'.$kwdTwo.'/s', $str, $matches);
Read: preg_match_all() and Pattern Modifiers.
正如Dor所说的,但是
<?php
$keyword1 = "this";
$keyword2 = "this";
$str = "this is my string this";
preg_match("/$keyword1(.*)$keyword2/s",$str,$matches);
echo $matches[1];
?>
输出:
is my string