0

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

Andy Lester
  • 91,102
  • 13
  • 100
  • 152

3 Answers3

0

How about just using plain js

var title = document.title

Or jquery

var title = $("head title")[0];
jh314
  • 27,144
  • 16
  • 62
  • 82
Joe Buckle
  • 871
  • 5
  • 14
  • 1
    thanks for the reply its for an external website i have managed to find my answer here http://stackoverflow.com/questions/4348912/get-title-of-website-via-link thank you –  Aug 12 '13 at 20:13
0

i found my answer here

Get title of website via link

$urlContents = file_get_contents("http://example.com/");

$dom = new DOMDocument();
@$dom->loadHTML($urlContents);

$title = $dom->getElementsByTagName('title');

print($title->item(0)->nodeValue . "\n"); // "Example Web Page"

thank you

Community
  • 1
  • 1
-1

I think there are character limits for the title. So title should not be in three lines. I have checked in Yoast plugin. It also saying the same. Then what is the purpose of getting three line seo title.

Shilpa
  • 1