我有一個url grabber設置,它工作正常。它抓住了一個文檔是在響應頭如的網址:PHP正則表達式查找字符串,直到
<script type='text/javascript' language='JavaScript'>
document.location.href = 'http\x3a\x2f\x2fcms.example.com\x2fd\x2fd\x2fworkspace\x2fSpacesStore\x2f61d96949-b8fb-43f1-adaf-0233368984e0\x2fFinancial\x2520Agility\x2520Report.pdf\x3fguest\x3dtrue'
</script>
這是我的採集腳本。
<?php
set_time_limit(0);
$target_url = $_POST['to'];
$html =file_get_contents($target_url);
$pattern = "/document.location.href = '([^']*)'/";
preg_match($pattern, $html, $matches, PREG_OFFSET_CAPTURE, 3);
$raw_url = $matches[1][0];
$eval_url = '$url = "'.$raw_url.'";';
eval($eval_url);
echo $url;
我們有一個變量添加到我們的文檔管理系統,使每個文檔的網址需要?來賓=真正的網址的結尾。當我們這樣做時,我的抓取器返回完整的URL並將其附加到文件名。所以我試圖讓它抓取url直到它碰到/ guest = true。使用此代碼:
<?php
set_time_limit(0);
$target_url = $_POST['to'];
$html =file_get_contents($target_url);
$pattern = "/document.location.href = '([^']*)\x3fguest\x3dtrue'/";
preg_match($pattern, $html, $matches, PREG_OFFSET_CAPTURE, 3);
$raw_url = $matches[1][0];
$eval_url = '$url = "'.$raw_url.'";';
eval($eval_url);
echo $url;
爲什麼它不會返回url直到?guest = true部分?又如何不工作?和什麼是修復?
當原始字符串包含'\ x3f'和'\ x3d'時,爲什麼要匹配'?'和'='?將後者變成前者是預期的。 – raina77ow
@ raina77ow是的,我嘗試完全匹配,這個副本和粘貼來自一個挫折嘗試。兩者都不能運作 – McPace