Solved

Retrieve TITLE tag data with PHP

Posted on 2010-11-22
7
302 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

737 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