Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 679
  • Last Modified:

regex to get content between title tags

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
lopband
Asked:
lopband
  • 2
  • 2
  • 2
2 Solutions
 
sh0eCommented:

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

Open in new window

0
 
sh0eCommented:
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
 
lopbandAuthor Commented:
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
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!

 
hieloCommented:
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
 
hieloCommented:
glad to help.
0
 
lopbandAuthor Commented:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now