Tuesday, October 25, 2016

How can I get the title of an HTML page using php?



How can I get the title of an HTML page using php? I've made a php web crawler and I want to implement this feature into my crawler so that it will have the name of the page and the url. Thanks in advance. Possibly using preg_match.


Answer




Would this help?



$myURL = 'http://www.google.com';
if (preg_match(
'/(.+)<\/title>/',<br/> file_get_contents($myURL),$matches) <br/> && isset($matches[1] )<br/> $title = $matches[1];<br/>else<br/> $title = "Not Found";<br/><br/></code></pre><br/> </div> <div style='clear: both;'></div> </div> <div class='post-footer'> <div class='post-footer-line post-footer-line-1'> <span class='post-author vcard'> </span> <span class='post-timestamp'> - <meta content='https://stklowf.blogspot.com/2016/10/how-can-i-get-title-of-html-page-using.html' itemprop='url'/> <a class='timestamp-link' href='https://stklowf.blogspot.com/2016/10/how-can-i-get-title-of-html-page-using.html?m=1&hl=en' rel='bookmark' title='permanent link'><abbr class='published' itemprop='datePublished' title='2016-10-25T02:04:00-07:00'>October 25, 2016</abbr></a> </span> <span class='post-comment-link'> </span> </div> <div class='post-footer-line post-footer-line-2'> <div class='mobile-link-button goog-inline-block' id='mobile-share-button'> <a href='javascript:void(0);'>Share</a> </div> </div> </div> </div> <div class='comments' id='comments'> <a name='comments'></a> <h4>No comments:</h4> <div id='Blog1_comments-block-wrapper'> <dl class='avatar-comment-indent' id='comments-block'> </dl> </div> <p class='comment-footer'> <div class='comment-form'> <a name='comment-form'></a> <h4 id='comment-post-message'> <a href='javascript:void(0)' id='Blog1_comment-editor-toggle-link'>Post a Comment</a></h4> <p> </p> <a href='https://www.blogger.com/comment/frame/8010773932506618868?po=3427682179354886614&hl=en&saa=85391&origin=https://stklowf.blogspot.com&m=1' id='comment-editor-src'></a> <iframe allowtransparency='true' class='blogger-iframe-colorize blogger-comment-from-post' frameborder='0' height='410px' id='comment-editor' name='comment-editor' src='' style='display: none' width='100%'></iframe> <script src='https://www.blogger.com/static/v1/jsbin/1345082660-comment_from_post_iframe.js' type='text/javascript'></script> <script type='text/javascript'> BLOG_CMT_createIframe('https://www.blogger.com/rpc_relay.html'); </script> </div> </p> </div> </div> </div> </div> </div> <div class='blog-pager' id='blog-pager'> <div class='mobile-link-button' id='blog-pager-newer-link'> <a class='blog-pager-newer-link' href='https://stklowf.blogspot.com/2016/10/plot-explanation-is-x-men-first-class_25.html?m=1&hl=en' id='Blog1_blog-pager-newer-link' title='Newer Post'>‹</a> </div> <div class='mobile-link-button' id='blog-pager-older-link'> <a class='blog-pager-older-link' href='https://stklowf.blogspot.com/2016/10/c-how-to-check-if-file-exists_25.html?m=1&hl=en' id='Blog1_blog-pager-older-link' title='Older Post'>›</a> </div> <div class='mobile-link-button' id='blog-pager-home-link'> <a class='home-link' href='https://stklowf.blogspot.com/?m=1&hl=en'>Home</a> </div> <div class='mobile-desktop-link'> <a class='home-link' href='https://stklowf.blogspot.com/2016/10/how-can-i-get-title-of-html-page-using.html?m=0&hl=en'>View web version</a> </div> </div> <div class='clear'></div> </div> </div> </div> </div> <div class='column-left-outer'> <div class='column-left-inner'> <aside> </aside> </div> </div> <div class='column-right-outer'> <div class='column-right-inner'> <aside> </aside> </div> </div> </div> <div style='clear: both'></div> <!-- columns --> </div> <!-- main --> </div> </div> <div class='main-cap-bottom cap-bottom'> <div class='cap-left'></div> <div class='cap-right'></div> </div> </div> <footer> <div class='footer-outer'> <div class='footer-cap-top cap-top'> <div class='cap-left'></div> <div class='cap-right'></div> </div> <div class='fauxborder-left footer-fauxborder-left'> <div class='fauxborder-right footer-fauxborder-right'></div> <div class='region-inner footer-inner'> <div class='foot section' id='footer-1'> </div> <!-- outside of the include in order to lock Attribution widget --> <div class='foot section' id='footer-3' name='Footer'><div class='widget Attribution' data-version='1' id='Attribution1'> <div class='widget-content' style='text-align: center;'> Powered by <a href='https://www.blogger.com' target='_blank'>Blogger</a>. </div> <div class='clear'></div> </div></div> </div> </div> <div class='footer-cap-bottom cap-bottom'> <div class='cap-left'></div> <div class='cap-right'></div> </div> </div> </footer> <!-- content --> </div> </div> <div class='content-cap-bottom cap-bottom'> <div class='cap-left'></div> <div class='cap-right'></div> </div> </div> </div> <script type='text/javascript'> window.setTimeout(function() { document.body.className = document.body.className.replace('loading', ''); }, 10); </script> <script type="text/javascript" src="https://www.blogger.com/static/v1/widgets/382300504-widgets.js"></script> <script type='text/javascript'> var BLOG_BASE_IMAGE_URL = 'https://resources.blogblog.com/img';var BLOG_LANG_DIR = 'ltr';window['__wavt'] = 'AOuZoY6mVmkt9xrF2_DXRfS1_m1QFieEVQ:1765848108355';_WidgetManager._Init('//www.blogger.com/rearrange?blogID\x3d8010773932506618868','//stklowf.blogspot.com/2016/10/how-can-i-get-title-of-html-page-using.html?m\x3d1\x26hl\x3den','8010773932506618868'); _WidgetManager._SetDataContext([{'name': 'blog', 'data': {'blogId': '8010773932506618868', 'title': 'Blog', 'url': 'https://stklowf.blogspot.com/2016/10/how-can-i-get-title-of-html-page-using.html?m\x3d1\x26hl\x3den', 'canonicalUrl': 'https://stklowf.blogspot.com/2016/10/how-can-i-get-title-of-html-page-using.html', 'homepageUrl': 'https://stklowf.blogspot.com/?m\x3d1\x26hl\x3den', 'searchUrl': 'https://stklowf.blogspot.com/search', 'canonicalHomepageUrl': 'https://stklowf.blogspot.com/', 'blogspotFaviconUrl': 'https://stklowf.blogspot.com/favicon.ico', 'bloggerUrl': 'https://www.blogger.com', 'hasCustomDomain': false, 'httpsEnabled': true, 'enabledCommentProfileImages': true, 'gPlusViewType': 'FILTERED_POSTMOD', 'adultContent': false, 'analyticsAccountNumber': '', 'encoding': 'UTF-8', 'locale': 'en', 'localeUnderscoreDelimited': 'en', 'languageDirection': 'ltr', 'isPrivate': false, 'isMobile': true, 'isMobileRequest': true, 'mobileClass': ' mobile', 'isPrivateBlog': false, 'isDynamicViewsAvailable': true, 'feedLinks': '\x3clink rel\x3d\x22alternate\x22 type\x3d\x22application/atom+xml\x22 title\x3d\x22Blog - Atom\x22 href\x3d\x22https://stklowf.blogspot.com/feeds/posts/default\x22 /\x3e\n\x3clink rel\x3d\x22alternate\x22 type\x3d\x22application/rss+xml\x22 title\x3d\x22Blog - RSS\x22 href\x3d\x22https://stklowf.blogspot.com/feeds/posts/default?alt\x3drss\x22 /\x3e\n\x3clink rel\x3d\x22service.post\x22 type\x3d\x22application/atom+xml\x22 title\x3d\x22Blog - Atom\x22 href\x3d\x22https://www.blogger.com/feeds/8010773932506618868/posts/default\x22 /\x3e\n\n\x3clink rel\x3d\x22alternate\x22 type\x3d\x22application/atom+xml\x22 title\x3d\x22Blog - Atom\x22 href\x3d\x22https://stklowf.blogspot.com/feeds/3427682179354886614/comments/default\x22 /\x3e\n', 'meTag': '', 'adsenseHostId': 'ca-host-pub-1556223355139109', 'adsenseHasAds': true, 'adsenseAutoAds': false, 'boqCommentIframeForm': true, 'loginRedirectParam': '', 'isGoogleEverywhereLinkTooltipEnabled': true, 'view': '', 'dynamicViewsCommentsSrc': '//www.blogblog.com/dynamicviews/4224c15c4e7c9321/js/comments.js', 'dynamicViewsScriptSrc': '//www.blogblog.com/dynamicviews/91113183d353c15a', 'plusOneApiSrc': 'https://apis.google.com/js/platform.js', 'disableGComments': true, 'interstitialAccepted': false, 'sharing': {'platforms': [{'name': 'Get link', 'key': 'link', 'shareMessage': 'Get link', 'target': ''}, {'name': 'Facebook', 'key': 'facebook', 'shareMessage': 'Share to Facebook', 'target': 'facebook'}, {'name': 'BlogThis!', 'key': 'blogThis', 'shareMessage': 'BlogThis!', 'target': 'blog'}, {'name': 'X', 'key': 'twitter', 'shareMessage': 'Share to X', 'target': 'twitter'}, {'name': 'Pinterest', 'key': 'pinterest', 'shareMessage': 'Share to Pinterest', 'target': 'pinterest'}, {'name': 'Email', 'key': 'email', 'shareMessage': 'Email', 'target': 'email'}], 'disableGooglePlus': true, 'googlePlusShareButtonWidth': 0, 'googlePlusBootstrap': '\x3cscript type\x3d\x22text/javascript\x22\x3ewindow.___gcfg \x3d {\x27lang\x27: \x27en\x27};\x3c/script\x3e'}, 'hasCustomJumpLinkMessage': false, 'jumpLinkMessage': 'Read more', 'pageType': 'item', 'postId': '3427682179354886614', 'pageName': 'How can I get the title of an HTML page using php?', 'pageTitle': 'Blog: How can I get the title of an HTML page using php?'}}, {'name': 'features', 'data': {}}, {'name': 'messages', 'data': {'edit': 'Edit', 'linkCopiedToClipboard': 'Link copied to clipboard!', 'ok': 'Ok', 'postLink': 'Post Link'}}, {'name': 'template', 'data': {'name': 'custom', 'localizedName': 'Custom', 'isResponsive': false, 'isAlternateRendering': true, 'isCustom': true}}, {'name': 'view', 'data': {'classic': {'name': 'classic', 'url': '?view\x3dclassic'}, 'flipcard': {'name': 'flipcard', 'url': '?view\x3dflipcard'}, 'magazine': {'name': 'magazine', 'url': '?view\x3dmagazine'}, 'mosaic': {'name': 'mosaic', 'url': '?view\x3dmosaic'}, 'sidebar': {'name': 'sidebar', 'url': '?view\x3dsidebar'}, 'snapshot': {'name': 'snapshot', 'url': '?view\x3dsnapshot'}, 'timeslide': {'name': 'timeslide', 'url': '?view\x3dtimeslide'}, 'isMobile': true, 'title': 'How can I get the title of an HTML page using php?', 'description': 'How can I get the title of an HTML page using php? I\x27ve made a php web crawler and I want to implement this feature into my crawler so that ...', 'url': 'https://stklowf.blogspot.com/2016/10/how-can-i-get-title-of-html-page-using.html?m\x3d1\x26hl\x3den', 'type': 'item', 'isSingleItem': true, 'isMultipleItems': false, 'isError': false, 'isPage': false, 'isPost': true, 'isHomepage': false, 'isArchive': false, 'isLabelSearch': false, 'postId': 3427682179354886614}}]); _WidgetManager._RegisterWidget('_HeaderView', new _WidgetInfo('Header1', 'header', document.getElementById('Header1'), {}, 'displayModeFull')); _WidgetManager._RegisterWidget('_BlogView', new _WidgetInfo('Blog1', 'main', document.getElementById('Blog1'), {'cmtInteractionsEnabled': false, 'mobile': true}, 'displayModeFull')); _WidgetManager._RegisterWidget('_FeaturedPostView', new _WidgetInfo('FeaturedPost1', 'main', document.getElementById('FeaturedPost1'), {}, 'displayModeFull')); _WidgetManager._RegisterWidget('_PopularPostsView', new _WidgetInfo('PopularPosts1', 'main', document.getElementById('PopularPosts1'), {}, 'displayModeFull')); _WidgetManager._RegisterWidget('_BlogSearchView', new _WidgetInfo('BlogSearch1', 'sidebar-right-1', document.getElementById('BlogSearch1'), {}, 'displayModeFull')); _WidgetManager._RegisterWidget('_BlogArchiveView', new _WidgetInfo('BlogArchive1', 'sidebar-right-1', document.getElementById('BlogArchive1'), {'languageDirection': 'ltr', 'loadingMessage': 'Loading\x26hellip;'}, 'displayModeFull')); _WidgetManager._RegisterWidget('_AttributionView', new _WidgetInfo('Attribution1', 'footer-3', document.getElementById('Attribution1'), {}, 'displayModeFull')); </script> </body> </html>