Solved

Retrieve TITLE tag data with PHP

Posted on 2010-11-22
7
296 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 30

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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 30

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now