Search code examples
phphtml-parsing

get the title of a page


i am getting the title of a page using the following

$urlContents = file_get_contents("$url");
preg_match("/<title>(.*)<\/title>/i", $urlContents, $matches);

the problem i am having is one of the sites the title is on 3 lines

<head id="head"><title>
    title goes here
</title><meta name="description" content="meta description" /> 

this is for a basic onpage seo tool i am writing, is there a better way i can get the title of a page?

Thank you


Solution

  • How about just using plain js

    var title = document.title
    

    Or jquery

    var title = $("head title")[0];