Solved

Retrieve TITLE tag data with PHP

Posted on 2010-11-22
7
299 Views
Last Modified: 2012-05-10
Is there a way with PHP to have it call a web page, preferably the main page, and grab the contents between these tags <TITLE>blah blah blah</TITLE>

0
Comment
Question by:VancRob
7 Comments
 
LVL 2

Accepted Solution

by:
benschwartz earned 125 total points
ID: 34190675
An easy way to grab the contents of a page is simply file_get_contents. using curl might be a more robust solution however.

If you are just parsing the contents of the title tag, a regex might be easiest, but for more complex parsing, the SimpleXML extension might be a good fit.

Here is an example regex.

<?php
$subject = "hello <title>world</title> how are you today";
$pattern = '/\<title\>(.*)\<\/title\>/';
preg_match($pattern, $subject, $matches);
print $matches[1];
0
 
LVL 24

Assisted Solution

by:mankowitz
mankowitz earned 125 total points
ID: 34190679
<?php
$subject = file_get_contents('http://www.example.com/');
$pattern = '/<title>(.*)<\/title>/i';
preg_match($pattern, $subject, $matches);
print_r($matches[1]);
?>
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 34190703
Use this:

<?php
$data=file_get_contents("www.webpage.com");
preg_match("#(<=<title>).*(?=</TITLE>)#", $data, $matches);
echo $matches[0];
?>

Cheers
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 6

Expert Comment

by:brb6708
ID: 34190721
if it's a valid XML file:

$fp=fopen("http://domain.com/file.xml","r");
$contents = stream_get_contents($fp);
fclose($fp);

if ($xml =  new SimpleXMLElement($contents)) {
    echo $xml->title;
} else {
    echo "no title";
}

if it's not a valid xml file:

$fp=fopen("http://domain.com/file.xml","r");
$contents = stream_get_contents($fp);
fclose($fp);

$p1 = strpos($contents,"<title>")+7;
$p2 = strpos($contents,"</title>");

if ($p2 > $p1) {
    echo substr($contents,$p1,$p2-$p1);
} else {
    echo "no title";
}
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 34190759
I'm arrived later and with a wrong regex: a record! But is $matches[0]
Bye
0
 
LVL 16

Expert Comment

by:CWS (haripriya)
ID: 36321048
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert html page to a php post email form automatically? 10 55
Dynamic varibles 5 32
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 33
PHP Syntax Error 4 27
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question