---
title: "利用PHP获取网页的源代码或标题"
author: "javaeyeren"
date: "2009-02-02T01:40:00Z"
tags: []
slug: "php"
draft: false
lastmod: "2009-02-02T01:40:00Z"
id: 601353
url: "https://javaeyeren.iteye.com/blog/601353"
---

获取网页源代码：

&lt;?php

$lines = file('http://www.gotogame.com.cn');

foreach ($lines as $line\_num =&gt; $line) {  
echo "Line &lt;b&gt;{$line\_num}&lt;/b&gt; : " . htmlspecialchars($line) . "&lt;br /&gt;\\n";  
}

?&gt;

获取网页的标题：

&lt;?  
$url = 'http://www.gotogame.com.cn';  
$lines\_array = file($url);  
$lines\_string = implode('', $lines\_array);  
eregi("&lt;head&gt;(.\*)&lt;/head&gt;", $lines\_string, $head);  
echo $head\[0];  
?&gt;