Fastest way to retrieve a title in PHP
<?php
function page_title($url) {
$fp = file_get_contents($url);
if (!$fp)
return null;
$res = preg_match("/<title>(.*)<\/title>/siU", $fp, $title_matches);
if (!$res)
return null;
// Clean up title: remove EOL's and excessive whitespace.
$title = preg_replace('/\s+/', ' ', $title_matches[1]);
$title = trim($title);
return $title;
}
?>
Gave 'er a whirl on the following input:
print page_title("http://www.google.com/");
Outputted: Google
Hopefully general enough for your usage. If you need something more powerful, it might not hurt to invest a bit of time into researching HTML parsers.
EDIT: Added a bit of error checking. Kind of rushed the first version out, sorry.
What is the fastest way to get millions of external titles and descriptions?
Have you considered companies that sell web-crawling-as-a-service? Fetching titles and metatags will be some of the simplest things they could do, beyond fetching tens of thousands of URLs per hour. Searching for crawling as a service has some links to such companies, and I expect they will be able to do it faster, and ultimately cheaper than you could yourself.
how to get page title using $_GET
You have the the lines in incorrect order, assigning $page_title
before $get
is set.
<?php
require_once('sp/conn.php');
require_once('sp/head.php');
require_once('sp/userbar.php');
$get = $_GET['name'];
$page_title = $get;
Note that your code is prone to SQL injection, see:
How can I prevent SQL injection in PHP?
How get page title dynamically in PHP
I think you can use get().
case 'post.php':
If($_GET["post_id"]== "1"){
$title="graphics design services";
$description= "about ";
}elseif($_GET["post_id"]== "2"){
}
break;
So use case to get the page post, the use get to get the ID.
Simplest way to parse a title from an HTML file using PHP functions only, no extra classes
With DOM:
<?php
$doc = new DOMDocument();
$doc->loadHTML(file_get_contents("1.html"));
$items = $doc->getElementsByTagName("title");
if($items->length > 0){
echo $items->item(0)->nodeValue;
}
?>
With Regular Expressions:
<?php
$html = file_get_contents('1.html');
preg_match("/<title>([^<]*)<\/title>/im", $html, $matches);
echo $matches[1];
?>
1.html
<html>
<head>
<title>This is the title</title>
</head>
<body>
<h1>Hello</h1>
</body>
</html>
Output:
This is the title
How can I get the title of an HTML page using php?
Would this help?
$myURL = 'http://www.google.com';
if (preg_match(
'/<title>(.+)<\/title>/',
file_get_contents($myURL),$matches)
&& isset($matches[1] )
$title = $matches[1];
else
$title = "Not Found";
Related Topics
Avoiding Form Resubmit in PHP When Pressing F5
How to Return Back Twice in Laravel
Is MySQL_Insert_Id Safe to Use
Fastest Way to Add Prefix to Array Keys
Why Do Changes to Some PHP Files Take So Long to Show on the Live Site
Mysql_Num_Rows() Expects Parameter 1 to Be Resource, Boolean Given In
Sorting Multidim Array: Prioritize If Column Contains Substring, Then Order by a Second Column
PHP Simplexml: Insert Node at Certain Position
Get All Instances of a Class in PHP
How to Pass Jquery Variables to PHP Variable
Redirect to Page and Send Custom Http Headers
Warning: Preg_Match() [Function.Preg-Match]: Unknown Modifier '/'
Pdostatement (Mysql): Inserting Value 0 into a Bit(1) Field Results in 1 Written in Table