To extract the content between two specified strings from a web page, you can use the following regular expression:
$pattern = '/string1(.*?)string2/is';This regular expression matches. string1 和 string2 The content between them, and use it. (.*?) Capture this content.
Here is an example code:
// 获取目标 URL 的 HTML 内容
$html = file_get_contents('https://example.com');
// 截取指定字符串之间的内容
$string1 = 'start';
$string2 = 'end';
$pattern = '/'.$string1.'(.*?)'.$string2.'/is';
if (preg_match($pattern, $html, $matches)) {
// 输出匹配结果
echo $matches[1];
} else {
echo '没有找到指定字符串之间的内容';
}In the above code, we use preg_match() The function matches the content between specified strings and returns the matching result. If no matching content is found between the specified strings, it returns the message "No content found between the specified strings."