How to search for html TITLE tag with PHP

I know this is probably a lame post, but i’ll do it anyway.

If you want to search for HTML title, you’ll notice everyone gives out an answer like this:

$matches = array();
$pattern = ‘/<title>(.*)<\/title>/’;
preg_match($pattern, $html, $matches);

$pagetitle = $matches[1];

But this actually doesn’t work in cases where there’s extra newline character. (.*) Doesn’t match new lines, which probably causes the problem. This means that it matches

<title> title </title>

but doesn’t match

<title> title
</title>

Instead, do this:

preg_match(“/<title>([^<]*)<\/title>/i”, $html_text, $title);

No related posts.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>