Display contents of the <title> tag of the current page using PHP

Asked

Viewed 543 times

-1

Hello, I would like a help to modify the code below:

<?php
function page_title($url) {
    $fp = file_get_contents($url);
    if (!$fp)
        return null;

    $res = preg_match("/<title>(.*)<\/title>/siU", $fp, $title_matches);
    if (!$res)
        return null;

    // Clean up title: remove EOL's and excessive whitespace.
    $title = preg_replace('/\s+/', ' ', $title_matches[1]);
    $title = trim($title);
    return $title;
}

?>

<?php $pagina = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>

The function of the code is to display the title of the current page and this purpose is being executed successfully, however all pages of the site whose code is present presents a very large slowness.

The original code was adapted from this reply https://stackoverflow.com/a/399357/5429657.

My idea when using this code is can give names to a variable for a plugin installed on the site. In every article created I included this text:

{{jlexhelpful name="PARAM 1" key="content_id" section_id="2" }}

PARAM 1 represents the name of the article, for example, Article 1, Article 2, Article 3... to avoid having to edit this text in all articles, I am using the request of the tag , that is, the 'PARAM 1' is written automatically when the user visits the page.

Trying to explain better what happens, I am putting on the pages the following codes, in addition to the two mentioned above:

<?php echo '{{jlexhelpful name="' ?> /*Insere {{jlexhelpful name=" na página*/
<?php print page_title("$pagina"); ?> /*Está parte é responsável por imprimir o título da página atual*/
<?php echo '" key="content_99votos99" section_id="2" }}<p>' ?> /*Final do texto, complementando o conteúdo que preciso*/

Thank you.

  • Example page with code sluggish (http://triviapw.hyperportal.blog.br/kya). Example without code quick (http://triviapw.hiperportal.blog.br)

  • 2

    This code makes a URL request each time it is executed, and will always be relatively slow. Click on [Edit] and explain better what you want to get, so we can propose a more decent alternative, and there’s a good chance it’s not what you need. The code presented only serves for cases where you do not have access to the pages locally. Take the opportunity to post the code in the question, external links besides being able to cease to exist in the future, end up forcing who will help to have to open a page just for this, and could be all here.

  • 1

    Could you elaborate on the scenario of this case?

  • @Bacco Thanks, I tried editing trying to detail my problem better.

  • Do you want the title of the page itself that PHP is running or other? If joomla is your own, it pays to get the titles straight from your system (via DB or something like).

  • @Bacco in this case I would use a javascript or anything else, menus a file_get_contents.

  • @Bacco I would like the title of the page itself to be displayed. At first I thought about working with PHP only, because I don’t have much knowledge about programming, and with the basic readings I find on the internet, I have better interpretation when it comes to php than database queries. The Joomla Facility is mine. I will wait some more time searching for other alternatives, in the last case return to the previous code where php brought me the article ID instead of the Title. Thank you.

Show 2 more comments

1 answer

0

Hello, I was able to solve the problem of slowness by changing the request made by the above mentioned codes as follows:

<?php
$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
if ($option=="com_content" && $view=="article") {
$ids = explode(':',JRequest::getString('id'));
$article_id = $ids[0];
$article =& JTable::getInstance("content");
$article->load($article_id);
echo $article->get("title");
}
?>

Finally the complete solution in my case was:

{{jlexhelpful name="<?php
$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
if ($option=="com_content" && $view=="article") {
$ids = explode(':',JRequest::getString('id'));
$article_id = $ids[0];
$article =& JTable::getInstance("content");
$article->load($article_id);
echo $article->get("title");
}
?>" key="content_<?php echo JRequest::getVar('id'); ?>" section_id="2" }}

As an example of the final result, it will appear as follows:

{{jlexhelpful name="Article Title" key="content_382" section_id="2" }}

Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.