J'ai mis 2 listes pour voir avec ton code et sa fonctionne. En quoi considères tu que ça ne fonctionne pas ?
J'ai quand même tenté de faire une fonction mais tout comme ton code elle remplace tout en un appel.
function replaceKeywords($content) {
return preg_replace_callback('/\{motscles=(.*)\}/Ui', static function ($matches) {
$escapedWords = array_map(
static function ($item) { return "|$item|"; },
explode(',', $matches[1])
);
$replacements = array_fill(
0,
count($escapedWords),
'<div class="mesmc"> <i class="fas fa-check top-6"></i>%s<img src="class-txt-img.php?img=%s"/></div>'
);
$num = 0;
$replacements = array_map(static function ($item) use (&$num) {
++$num;
return sprintf($item, 1 === $num ? ' ' : '', $num);
}, $replacements);
return str_replace(
$escapedWords,
$replacements,
implode('', $escapedWords)
);
}, $content);
}
$contenu3 = '{motscles=Jean, Paul, Pierre, Jean Jacques, Romain, Pierre Henri}
{motscles=Jean, Paul, Pierre, Jean Jacques, Romain, Pierre Henri}';
echo replaceKeywords($contenu3);
EDIT : si tu souhaites que la variable img soit égale au mot clé (vu le script txt-img.php), la fonction corrigée (et effectivement j'ai vu qu'avec plusieurs listes de mots clés différents ton code ne fait pas tout le job) :
function replaceKeywords($content) {
return preg_replace_callback('/\{motscles=(.*)\}/Ui', static function ($matches) {
$words = explode(',', $matches[1]);
$replacements = array_map(static function ($index, $word) {
return sprintf(
'<div class="mesmc"> <i class="fas fa-check top-6"></i>%s<img src="class-txt-img.php?img=%s" /></div>',
0 === $index ? ' ' : '',
urlencode(trim($word))
);
}, array_keys($words), $words);
return implode('', $replacements);
}, html_entity_decode($content));
}
$content = '{motscles=Jean, Paul, Pierre, Jean Jacques, Romain, Pierre Henri}
{motscles=Riri, Fifi, Loulou, Donald, Mickey}';
echo replaceKeywords($content);
PS : à noter, avec l'utilisation du urlencode il faudrait que dans txt-img.php tu utilises urldecode sur get img.
Du coup j'ai fait mumuse et j'ai corrigé le script de génération des images :
<?php
$text = $_GET['img'] ?? null;
if (!$text) {
exit;
}
$text = html_entity_decode(urldecode($text));
$font = 4;
$image = imagecreate(imagefontwidth($font) * strlen($text), imagefontheight($font));
imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
imagestring($image, $font, 0, 0, $text, $black);
header("Content-type: image/png");
echo imagepng($image);
La génération d'image avec imagettftext pour supporter les caractères spéciaux :
<?php
$text = $_GET['img'] ?? null;
if (!$text) {
exit;
}
$text = html_entity_decode(urldecode($text));
putenv('GDFONTPATH=' . realpath('.'));
$fontName = 'Arial.ttf';
$fontSize = 11;
$box = imagettfbbox($fontSize, 0, $fontName, $text);
$min_x = min([$box[0], $box[2], $box[4], $box[6]]);
$max_x = max([$box[0], $box[2], $box[4], $box[6]]);
$width = ($max_x - $min_x);
$height = $fontSize * 1.6;
$image = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 0, 0, $width, $height, $white);
imagettftext($image, $fontSize, 0, 0, $fontSize + 1, $black, $fontName, $text);
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);