Solved

regex to get content between title tags

Posted on 2008-10-11
6
651 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
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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
The viewer will learn how to dynamically set the form action using jQuery.

740 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