要截取網頁中,兩個指定字符串中間的內容,你可以使用以下正則表達式:
$pattern = '/string1(.*?)string2/is';該正則表達式匹配 string1 和 string2 之間的內容,並使用 (.*?) 捕獲這些內容。
以下是一個示例代碼:
// 獲取目標 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 '沒有找到指定字符串之間的內容';
}在上述代碼中,我們使用 preg_match() 函數來匹配指定字符串之間的內容,並輸出匹配結果。如果沒有找到指定字符串,則輸出“沒有找到指定字符串之間的內容”。