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
Solved

Retrieve TITLE tag data with PHP

Posted on 2010-11-22
7
300 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
How to display div value on input field value in php 11 53
parse url to form? 7 25
Ahax pagination 9 32
Checking https returns 301 21 21
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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

860 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