vesper Posted September 15, 2008 Share Posted September 15, 2008 Hi there, I've got a piece of code that let's me obtain the title of a page using a given URL. However, if the title tag has any other code in it, for example <title id="something">, it does not work because the preg_match is looking for just the open and closing tags. My question is: how can I get the code to ignore anything between '<title' and '>'?? The preg_match currently looks like this: preg_match("/<title>(.+)<\/title>/i",$file,$m) Thanks. Link to comment https://forums.phpfreaks.com/topic/124295-getting-a-title-using-a-url/ Share on other sites More sharing options...
metrostars Posted September 15, 2008 Share Posted September 15, 2008 I'm not very good at regex. In fact I hate it. but try (untested): preg_match("/<title([.]{0,})>(.+)<\/title>/i",$file,$m) This will match <title 'then anything in here' >Blah</title> Link to comment https://forums.phpfreaks.com/topic/124295-getting-a-title-using-a-url/#findComment-641856 Share on other sites More sharing options...
vesper Posted September 15, 2008 Author Share Posted September 15, 2008 Hi metrostars, I just tried it, unfortunately it didn't work. Thanks for the reply though. Link to comment https://forums.phpfreaks.com/topic/124295-getting-a-title-using-a-url/#findComment-641858 Share on other sites More sharing options...
JasonLewis Posted September 15, 2008 Share Posted September 15, 2008 Try this one out: preg_match("/<title.*?>(.*?)<\/title>/i", $file, $m); Link to comment https://forums.phpfreaks.com/topic/124295-getting-a-title-using-a-url/#findComment-641859 Share on other sites More sharing options...
vesper Posted September 15, 2008 Author Share Posted September 15, 2008 It worked! Superb! Thanks for that! Hugely appreciated - headache seeping away now! Link to comment https://forums.phpfreaks.com/topic/124295-getting-a-title-using-a-url/#findComment-641860 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.