Solved

regex to get content between title tags

Posted on 2008-10-11
6
660 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 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

615 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