正则表达式用于排除标题标签之间的内容


Regex to exclude content between title tag

这个正则表达式排除标题标签的内容有什么问题?

$plaintext = preg_match('#<title>(.*?)</title>#', $html);

$html有整个页面的html代码。

听起来你从来没有得到一个有效的答案。让我们删除标题标签。

搜索: (?s)<title>.*?</title>

替换:""

法典:

$regex = "~(?s)<title>.*?</title>~";
$ replaced = preg_replace($regex,"",$pagecontent);

解释正则表达式

(?s)                     # set flags for this block (with . matching
                         # 'n) (case-sensitive) (with ^ and $
                         # matching normally) (matching whitespace
                         # and # normally)
<title>                  # '<title>'
.*?                      # any character (0 or more times (matching
                         # the least amount possible))
</title>                 # '</title>'

这将获取两个标签之间的所有内容

preg_match('<title>.+', $html);

我想它应该是这样的......这只给你介于两者之间的内容

preg_match('(?<=<title>).*(?=<'/title>)', $html);

http://www.phpliveregex.com/p/1SJ

http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/