从html中提取标题标签 [英] extract title tag from html

查看:102
本文介绍了从html中提取标题标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从html字符串中提取标题标签的内容。我做了一些搜索,但到目前为止我无法在VB / C#或PHP中找到这样的代码。这也应该适用于大写和小写标签,例如应与< title>< / title> 和< TITLE>< / title> 一起使用。谢谢。

I want to extract contents of title tag from html string. I have done some search but so far i am not able to find such code in VB/C# or PHP. Also this should work with both upper and lower case tags e.g. should work with both <title></title> and <TITLE></TITLE>. Thank you.

推荐答案

你可以使用正则表达式来实现这个功能,但它不是完全防错的。它会做,如果你只是想简单的东西(在PHP中):

You can use regular expressions for this but it's not completely error-proof. It'll do if you just want something simple though (in PHP):

function get_title($html) {
  return preg_match('!<title>(.*?)</title>!i', $html, $matches) ? $matches[1] : '';
}

这篇关于从html中提取标题标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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