Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get title of website via link

Notice how Google News has sources on the bottom of each article excerpt.

The Guardian - ABC News - Reuters - Bloomberg

I'm trying to imitate that.

For example, upon submitting the URL http://www.washingtontimes.com/news/2010/dec/3/debt-panel-fails-test-vote/ I want to return The Washington Times

How is this possible with php?

like image 536
Noob Avatar asked Dec 03 '10 19:12

Noob


People also ask

How do you find the title of a website?

On web browsers, the website title appears at the top of the tab or window, and in search results website titles display as bold hyperlinked texts. A good rule of thumb is to make website titles 50 to 65 characters long and ensure they are clear, as well as descriptive without being truncated.

How do you give a title a link?

Use the title attribute for your links when you can provide additional information about that link and/or the page it goes to. For example, if your anchor text just says click here, give your users a better idea of what they'll get if they click on the link (scroll over the “click here” link to see what I mean).

What is a URL title?

A website title, or title tag, is an HTML element that specifies the content of a webpage. A website title is helpful for both users and search engines. An internet user needs a website title so they can see an accurate and concise description of a page's content before clicking on a link in the SERPs.

How do I find the title tag in HTML?

You can find your page's title tag within the <head> section of the page's HTML markup.


1 Answers

My answer is expanding on @AI W's answer of using the title of the page. Below is the code to accomplish what he said.

<?php  function get_title($url){   $str = file_get_contents($url);   if(strlen($str)>0){     $str = trim(preg_replace('/\s+/', ' ', $str)); // supports line breaks inside <title>     preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title); // ignore case     return $title[1];   } } //Example: echo get_title("http://www.washingtontimes.com/");  ?> 

OUTPUT

Washington Times - Politics, Breaking News, US and World News

As you can see, it is not exactly what Google is using, so this leads me to believe that they get a URL's hostname and match it to their own list.

http://www.washingtontimes.com/ => The Washington Times

like image 150
Jose Vega Avatar answered Sep 23 '22 12:09

Jose Vega