-1

I need to fetch the contents under MY TITLE to appear as title within an image on the same page. For example this is a link from my blog and the title of the page appears within the image.

Just a code to fetch my title and use it within an image without having to copy paste manually every time. The structure of my URL is (title.php)

1 Answers1

1

I presume you meant the page's title at the browser's tab, right? (question was a bit unclear)

If so, this value can be accessed in Javascript using

document.title

This is the cleanest way to get it, and the most effective one. There is a weird play if you want to use purely PHP to get it, however, I would stick to using this method if I were you.

For reference, here is the previous SO topic on getting page title in PHP

How to get page title in php?


If you wanted to use that value in PHP, you could use Javascript inside PHP by simply writing

echo '<script type="text/javascript"> document.title </script>';
Patrick Younes
  • 139
  • 1
  • 15
  • I'm not a pro when it comes to coding. How do I use document.title within a html page? – Nina Larissa Feb 26 '20 at 05:34
  • I understand that you want a quick and easy answer, however me providing it would only damage you on the longer term. I would highly recommend checking out a course on Javascript ES6 Essentials. This is related to the chapter DOM Manipulation, however I'm sure you'll require the whole course and not to skip anything through it. https://www.linkedin.com/learning/javascript-essential-training-3 is a course I would recommend. – Patrick Younes Feb 26 '20 at 06:43
  • Thanks a lot,,really appreciate this. Will definitely spend time to learn. – Nina Larissa Feb 26 '20 at 20:44