Display content of the title tag of the current page using PHP

0

Hello, I'd like some help modifying 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 running successfully, but all pages of the site whose code is present are very slow.

The original code has been adapted from this response link .

My idea when using this code is you can name a variable for a plugin installed on the site. In every article I 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 ... so I do not have to edit this text in all articles, I am using the tag request, ie 'PARAM 1 'is automatically written when the user visits the page.

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

<?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.

    
asked by anonymous 09.08.2016 / 01:11

1 answer

0

Hello, I was able to solve the problem of slowness by changing the request made by the codes mentioned above 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.

    
09.08.2016 / 04:47