网站域名获取标题PHP代码
以下是获取网站标题的PHP代码:
$url = "https://www.example.com";
$html = file_get_contents($url);
preg_match("/<title>(.*)<\/title>/i", $html, $matches);
$title = $matches[1];
echo $title;
解释:
- 定义一个变量
$url存储要获取标题的网站的URL。 - 使用
file_get_contents()函数获取网站的HTML代码。 - 使用正则表达式
/<title>(.*)<\/title>/i匹配 HTML 中的标题标签,并将标题存储在$matches数组中。 - 从
$matches数组中获取标题,并将其存储在$title变量中。 - 使用
echo函数输出标题。
原文地址: https://www.cveoy.top/t/topic/bJOj 著作权归作者所有。请勿转载和采集!