TShopping

 找回密碼
 註冊
搜索
查看: 1193|回復: 0

[教學] file_get_contents取得完整內容

[複製鏈接]
發表於 2019-9-12 15:03:35 | 顯示全部樓層 |閱讀模式
 
Push to Facebook
在處理檔案時,有時候會需要抓取整個檔案全部的內容,PHP提供了file_get_contents這個函式可以一次抓取完整檔案內容

  1. $file = file_get_contents("test.txt");
複製代碼

另外一般寫一些crawler多少都會用到截取某個網頁某個區塊的功能(使用file_get_contents抓取網頁雖然方便,但是效能與安全性都非常糟,所以建議還是使用curl)

  1. $url = "http://127.0.0.1/index.php" ;
  2. //取得該網頁html
  3. $content = file_get_contents($url);

  4. //搜尋<label>標籤內容
  5. //單筆資料(s代表可接受換行)
  6. preg_match("/<label>.*?</label>/s",$content,$matches);
  7. print_r($matches);

  8. //全文搜尋
  9. preg_match_all("/<label>.*?</label>/s",$content,$matches);
  10. print_r($matches);
複製代碼
參考文章:https://blog.johnsonlu.org/php%E5%8F%96%E5%BE%97%E7%B6%B2%E9%A0%81%E5%85%A7%E5%AE%B9-%E5%8F%8A-%E6%90%9C%E5%B0%8B%E5%AD%97%E4%B8%B2/

 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-3-29 21:43 , Processed in 0.051748 second(s), 23 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表