Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

regex to get content between title tags

Posted on 2008-10-11
6
Medium Priority
?
670 Views
Last Modified: 2013-12-13
Hello,

Basically, I am doing a fget() to get a remote page, then i need a regex (which i cant figure) to read the text between
<title> .. and </title>

for example:
<title> I am a fancy title </title>

running the regex should give me:
I am a fancy title

Thanks!
0
Comment
Question by:lopband
[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
  • 2
  • 2
  • 2
6 Comments
 
LVL 16

Expert Comment

by:sh0e
ID: 22695655

preg_match('/<title>(.*?)</title>/', "<title> I am a fancy title </title>", $matches);
print $matches[1];

Open in new window

0
 
LVL 16

Assisted Solution

by:sh0e
sh0e earned 248 total points
ID: 22695664
Forgot to escape.
preg_match('/<title>(.*?)<\/title>/', "<title> I am a fancy title </title>", $matches);
print $matches[1];
 
eregi('<title>(.*?)</title>', "<title> I am a fancy title </title>", $matches);
print $matches[1];

Open in new window

0
 
LVL 1

Author Comment

by:lopband
ID: 22695723
Hello,
I think you misunderstood me... I have no idea what the title will say, " I am a fancy title" was just an example.

It should work with any title...
unless thats what it already does..I have not tested your code as yet.. ;)
0
Industry Leaders: 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 82

Accepted Solution

by:
hielo earned 252 total points
ID: 22696033
title may be upper and/or lower case letters. Make sure you do a case insensitive match:
$data = file_get_contents("http://www.somesite.com/somepage.html");
preg_match('/#<title>([^<]*)</title>#/iU',$data,$match);
$title=$match[1]; 
echo $title;

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 22696502
glad to help.
0
 
LVL 1

Author Comment

by:lopband
ID: 22697782
Hello Heilo,
I am getting this error when i run your code:

Warning: preg_match() [function.preg-match]: Unknown modifier 't' in C:\wamp\www\ezee\tests\get_remote_title.php on line 3

This is the entire code:
<?php
$data = file_get_contents("http://www.youtube.com/watch?v=oQ2dKXGAjNg");
preg_match('/#<title>([^<]*)</title>#/iU',$data,$match);
$title=$match[1];
echo $title;
?>
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
This article discusses how to create an extensible mechanism for linked drop downs.
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 count occurrences of each item in an array.

721 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