Solved

regex to get content between title tags

Posted on 2008-10-11
6
645 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
  • 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 62 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
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 82

Accepted Solution

by:
hielo earned 63 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
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…

746 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

13 Experts available now in Live!

Get 1:1 Help Now