PHP code problems in my plugin Topic is solved

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
sniperpike
Joomla! Apprentice
Joomla! Apprentice
Posts: 34
Joined: Thu Aug 12, 2010 2:49 pm

PHP code problems in my plugin

Post by sniperpike » Thu Mar 16, 2023 11:10 am

Hi there,

I have made a plugin (In fact, in cant code from scratch but Chat GPT did it for me :) )
It should do the following:
Get image and title from another article (through a link)
And show the image as thumbnail and title to the right of the image.
It works, but not flawless. I also get "ItemAuthor" attached to the title.
You can see the plugin on my website here: https://min-barsel.dk/sovn/sovnprobleme ... ng-pa-spil

It is right under the text "Here is the plugin"

Image and title is fetched from this url: https://min-barsel.dk/sundhed/gravidite ... behandling

But as you can see, it also fetch "ItemAuthor" (In this case it says "Skrevet af Min Barsel")
I only want the title "Graviditetssukkersyge: Årsager og Behandling"

Here is the PHP code:
<?php
// No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.plugin.plugin');

class plgK2K2RelatedLinks extends JPlugin
{
public function __construct(&$subject, $config)
{
parent::__construct($subject, $config);
}

public function onK2BeforeDisplay(&$item, &$params, $limitstart)
{
$regex = '#{link="(.*?)"}#s';
preg_match_all($regex, $item->text, $matches, PREG_SET_ORDER);

if ($matches) {
foreach ($matches as $match) {
$link = $match[1];
$linkContent = $this->fetchLinkData($link);

if ($linkContent) {
$replacement = '<div class="related-link-thumbnail">'
. '<a href="' . $link . '">'
. '<img src="' . $linkContent['image'] . '" alt="' . $linkContent['title'] . '"/>'
. '<div class="related-link-text">'
. '<span class="read-also">Læs også</span>'
. '<h2>' . $linkContent['title'] . '</h2>'
. '</div>'
. '</a>'
. '</div>';

$item->text = str_replace($match[0], $replacement, $item->text);
} else {
$item->text = str_replace($match[0], '', $item->text);
}
}
}
}

private function fetchLinkData($url)
{
$data = [];

$html = @file_get_contents($url);

if ($html === false) {
error_log('Error fetching data from URL: ' . $url);
return false;
}

libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTML($html);
libxml_clear_errors();

$xpath = new DOMXPath($dom);


$titleNode = $xpath->query('//h1[@class="itemTitle"]')->item(0);
$imageNode = $xpath->query('//meta[@property="og:image"]')->item(0);

if ($titleNode && $imageNode) {
$title = $titleNode->nodeValue;
$title = strip_tags($title); // Remove any HTML tags from the title
$data['title'] = $title;
$data['image'] = $imageNode->getAttribute('content');
} else {
error_log('Error parsing data from URL: ' . $url);
return false;
}

return $data;
}
}
?>

I hope someone is able to help as Chat GPT cant figure out the problem.

Sincerely,
Martin

sniperpike
Joomla! Apprentice
Joomla! Apprentice
Posts: 34
Joined: Thu Aug 12, 2010 2:49 pm

Re: PHP code problems in my plugin

Post by sniperpike » Thu Mar 16, 2023 11:18 am

Sorry for the inconvenience, but Chat GPT finally found a solution, and added the following lines:
$title_parts = explode('Skrevet af', $title);
$title = trim($title_parts[0]);

That worked :)

Sincerely,
Martin


Locked

Return to “Joomla! 3.x Coding”