Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Retrieve TITLE tag data with PHP

Posted on 2010-11-22
7
Medium Priority
?
306 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 500 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 500 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
Technology Partners: 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

604 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