...

View Full Version : Simple Regex Help



BRUm
08-28-2006, 09:18 AM
Hi,

I cam accross this regex pattern:


<TAGb[^>]*>(.*?)</TAG>

I want to grab titles from websites, so I edited to get:


<titleb[^>]*>(.*?)</title>

Could anyone please explain why it doesn't work?

Thanks.

(Note: The forum won't let me put backslashes, there should be one before the "b")

Nimlhg
08-28-2006, 09:38 AM
What you're looking for is this:

$pattern = '<title>(.+)</title>';

Might want to make sure that you set preg_match (or whatever you're using) to being case-insensitive.

BRUm
08-28-2006, 09:54 AM
Hm I just tried that, and I still only get
Array ( )

GJay
08-28-2006, 12:07 PM
the pattern should have delimiters, i.e:


$pattern='#<title>(.+)</title>#';


And this pattern won't let the <title> have any attributes, for example:
<title id="theid">
won't match, is that an issue?
showing your code is always helpful...

BRUm
08-28-2006, 12:33 PM
Title has attributes? I just need to match the basic HTML <title> tag.

Example:


<title>This is my website</title>

The pattern should return (When array is printed):


This is my website

But it doesn't.

chump2877
08-28-2006, 01:44 PM
Like GJay said, for the most part:


$page_content = file_get_contents('MyPage.php');
$pattern = '/(<title>)(.+?)(<\/title>)/i';
preg_match($pattern,$page_content,$matches);

echo "The title tag content is: ".$matches[2];

BRUm
08-28-2006, 02:07 PM
Hi chump,

Thanks for your help, but I tried your code by itself and I get an error on your preg_match line. You missed off a ; but I still get the error after I corrected it. :S

chump2877
08-28-2006, 02:33 PM
Hi chump,

Thanks for your help, but I tried your code by itself and I get an error on your preg_match line. You missed off a ; but I still get the error after I corrected it. :S

Sorry about the semi-colon...I added that in, and it works fine now for me...what error are you getting?

BRUm
08-28-2006, 03:00 PM
Ah, it seems I had saved the script to my C:\ whereas I was uploaded the old script thinking I had updated it :D lol the script works, thank you :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum