ø Don’t waste your time reading this blog ø

How to search for html TITLE tag with PHP

Filed under: web 2.0 — Tags: — taewoo @ 12:24 pm June 27, 2009

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);

[Post to Twitter] Tweet This Post 

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment