rammac13 Posted October 20, 2008 Share Posted October 20, 2008 Does anyone know of a way to detect what is in the title tag of a page? What I am attempting to do is have some code that will detect whatever the title of my page is and input it directly into a tag cloud that will be at the bottom of the page. Any Ideas? In any Language? Quote Link to comment https://forums.phpfreaks.com/topic/129320-detect-title-tag/ Share on other sites More sharing options...
dropfaith Posted October 20, 2008 Share Posted October 20, 2008 all i can think of would be a php include <title><?php include("title.html"); ?></title> then include it again anywhere on your page the same way.. all pages need to be php pages tho page.php Quote Link to comment https://forums.phpfreaks.com/topic/129320-detect-title-tag/#findComment-670458 Share on other sites More sharing options...
dropfaith Posted October 20, 2008 Share Posted October 20, 2008 also here is more info on ways to do it Sometimes, you might want to get the current page URL that is shown in the browser URL window. For example if you want to let your visitors submit a blog post to Digg you need to get that same exact URL. There are plenty of other reasons as well. Here is how you can do that. Add the following code to a page: <?php function curPageURL() { $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";} $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } return $pageURL; } ?> You can now get the current page URL using the line: <?php echo curPageURL(); ?> Sometimes it is needed to get the page name only. The following example shows how to do it: <?php function curPageName() { return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1); } echo "The current page name is ".curPageName(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/129320-detect-title-tag/#findComment-670462 Share on other sites More sharing options...
haku Posted October 20, 2008 Share Posted October 20, 2008 <?php $title = 'title'; ?> <title><?php echo $title; ?></title> you now have the title in a variable called $title; Quote Link to comment https://forums.phpfreaks.com/topic/129320-detect-title-tag/#findComment-670469 Share on other sites More sharing options...
wrongmove18 Posted October 21, 2008 Share Posted October 21, 2008 try this... <?php $contents = file_get_contents("page.htm"); preg_match("/\<title\>(.*)\<\/title\>/i", $contents, $results); echo $results[1]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/129320-detect-title-tag/#findComment-670701 Share on other sites More sharing options...
gethinw Posted October 21, 2008 Share Posted October 21, 2008 Or, in javascript: <script type='text/javascript'> document.write(document.title); </script> Quote Link to comment https://forums.phpfreaks.com/topic/129320-detect-title-tag/#findComment-670703 Share on other sites More sharing options...
rammac13 Posted October 21, 2008 Author Share Posted October 21, 2008 Ok so I think I am getting close. I am trying to put the title name (link) into a link on the page. but I am not sure how to call it correctly. I have javascript:tagLink in the a href link but that is not correct. What am I missing. Thanks. <html> <head> <title> link </title> <script type="text/javascript"> function getTagCloudLink() { var tagLink = document.getElementsByTagName("title")[0].innerHTML ; } </script> </head> <body> <p><a href="shop-enzy.com/store/index.html()">javascript:tagLink</a></p> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/129320-detect-title-tag/#findComment-670997 Share on other sites More sharing options...
haku Posted October 21, 2008 Share Posted October 21, 2008 var tagLink = document.getElementsByTagName("title").firstChild.data Quote Link to comment https://forums.phpfreaks.com/topic/129320-detect-title-tag/#findComment-671331 Share on other sites More sharing options...
gethinw Posted October 22, 2008 Share Posted October 22, 2008 Why not just use <p><a href='...'><script type='text/javascript'>document.write(document.title)</script></a></p> Seems a lot simpler to me. Quote Link to comment https://forums.phpfreaks.com/topic/129320-detect-title-tag/#findComment-671651 Share on other sites More sharing options...
haku Posted October 22, 2008 Share Posted October 22, 2008 Simpler or not, it's outdated, and requires combining your javascript in your html, which goes against the practice these days of keeping your HTML, CSS and Javascript separate and in separate files. I think document.write may even be deprecated, though I'm not 100% sure of that. But I would never use it. Quote Link to comment https://forums.phpfreaks.com/topic/129320-detect-title-tag/#findComment-671698 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.