我如何使用PHP获取HTML页面的标题? [英] How can I get the title of an HTML page using php?

查看:328
本文介绍了我如何使用PHP获取HTML页面的标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用php获取HTML页面的标题?我做了一个PHP的网络爬虫,我想要实现这个功能到我的爬虫,这样它就会有页面和网址的名称。提前致谢。可能使用preg_match。

解决方案



pre > $ myURL ='http://www.google.com';
if(preg_match(
'/<title>(.+)<\/title>/',
file_get_contents($ myURL),$ matches)
& amp ;& isset($ matches [1])$ ​​b $ b $ title = $ matches [1];
else
$ title =Not Found;


How can I get the title of an HTML page using php? I've made a php web crawler and I want to implement this feature into my crawler so that it will have the name of the page and the url. Thanks in advance. Possibly using preg_match.

解决方案

Would this help?

$myURL = 'http://www.google.com';
if (preg_match(
        '/<title>(.+)<\/title>/',
        file_get_contents($myURL),$matches) 
    && isset($matches[1] )
   $title = $matches[1];
else
   $title = "Not Found";

这篇关于我如何使用PHP获取HTML页面的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆