/    Sign up×
Community /Pin to ProfileBookmark

Hi, I have been trying extract the title from html pages, i’m surprised that there isn’t a function to do this in a similar way that get_meta_tags gets descriptions and keywords. Anyway i’ve used regex to search for the title tags but i can’t get the title content into a variable. Can anyone see where i’m going wrong?

code:-
$URL = $_GET[‘url’];
$page = file_get_contents(“$URL”);
preg_match_all( “/<title>([a-z 0-9]*)</title>/”, $page, $matches );

$title = $matches[0][0];

echo $title;

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 15.2006 — [code=php]
if(preg_match('/<title>([^<]+)</title>/i', $page, $matches))
{
$title = $matches[1]; // 1st sub-pattern is index 1
}
else
{
$title = "Untitled";
}
[/code]
Copy linkTweet thisAlerts:
@bokehAug 15.2006 — [I]<title>[/I] can take attributes. [URL=http://www.w3.org/TR/html401/sgml/dtd.html#i18n]%i18n[/URL]
×

Success!

Help @Solaar spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...