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")
Nimlhûg
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.
Hm I just tried that, and I still only get Array ( )
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...
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];
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?
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 :)