2012-10-11 5 views
0

Je crée un générateur de mème, Imagick générant les images. Mon problème est que même si je fais certaines opérations sur la chaîne que j'utilise pour l'image, la sortie est incorrecte.strip_tags et trim ne fonctionnent pas correctement

Par exemple:

$_POST['text_top'] = " test test<br>"; //(starts with a space) 

Alors je fais:

$text_top = strip_tags(trim($_POST['text_top'])); 

Mais sur l'écran de text_top $, après que je colle la variable sur l'image, je reçois:

&nbsptest test&lt;br&gt; 

Pourquoi cela se passe-t-il si j'appelle strip_tags et trim, d'après ce que je vois, corrige et comme d'habitude?

Tout est codé en UTF8.

Merci!

EDIT: (code complet)

function wordWrapAnnotation(&$image, &$draw, $text, $maxWidth) 
{ 
$words = explode(" ", $text); 
$lines = array(); 
$i = 0; 
$lineHeight = 0; 
while($i < count($words)) 
{ 
    $currentLine = $words[$i]; 
    if($i+1 >= count($words)) 
    { 
     $lines[] = $currentLine; 
     break; 
    } 
    //Check to see if we can add another word to this line 
    $metrics = $image->queryFontMetrics($draw, $currentLine . ' ' . $words[$i+1]); 
    while($metrics['textWidth'] <= $maxWidth) 
    { 
     //If so, do it and keep doing it! 
     $currentLine .= ' ' . $words[++$i]; 
     if($i+1 >= count($words)) 
      break; 
     $metrics = $image->queryFontMetrics($draw, $currentLine . ' ' . $words[$i+1]); 
    } 
    //We can't add the next word to this line, so loop to the next line 
    $lines[] = $currentLine; 
    $i++; 
    //Finally, update line height 
    if($metrics['textHeight'] > $lineHeight) 
     $lineHeight = $metrics['textHeight']; 
} 
return array($lines, $lineHeight); 
} 

$text_top = strip_tags(trim($_REQUEST['text_top'])); 
$text_bottom = strip_tags(trim($_REQUEST['text_bottom'])); 
$id_base = trim($_REQUEST['id_base']); 

/* Création d'un nouvel objet imagick */ 
$im = new Imagick($_REQUEST['image']); 

/* Création d'un nouvel objet imagickdraw */ 
$draw = new ImagickDraw(); 

/* Définition de la taille du texte à 52 */ 
$draw->setFontSize(52); 
$draw->setTextAlignment(2); 
$draw->setFont("impact.ttf"); 
$draw->setFillColor('white'); 
$draw->setStrokeColor("black"); 
$draw->setStrokeWidth(1); 

/* Ajout d'un texte */ 
//$draw->annotation($im->getImageWidth()/2, 50, $text); 
list($lines, $lineHeight) = wordWrapAnnotation($im, $draw, stripslashes($text_top), $im->getImageWidth()); 
$posY= 50; 
for($i = 0; $i < count($lines); $i++){ 
$draw->annotation($im->getImageWidth()/2, $posY + $i*$lineHeight, $lines[$i]); 
} 

$im->drawImage($draw); 
+2

Montrez votre code complet. Il y a probablement un 'htmlspecialchars()' qui se passe quelque part qui provoque ce –

+0

j'ai édité, j'ai également inclus la fonction que j'utilise pour faire le mot enveloppant sur l'image. –

Répondre

5

vous pouvez essayer cela?

$text_top = strip_tags(trim(html_entity_decode($_POST['text_top'], ENT_QUOTES, 'UTF-8'), "\xc2\xa0")); 

Il semble que votre chaîne soit codée en HTML.

EDIT

Ajout du support pour l'encodage UTF-8. De cette façon, l'espace insécable est réduit correctement, au lieu de donner ?.

De la documentation de PHP html_entity_decode:

Note:

On peut se demander pourquoi TRIM (html_entity_decode (' ')); ne pas réduire la chaîne à une chaîne vide, parce que l'entité ' ' n'est pas le code ASCII 32 (qui est dépouillé par trim()) mais le code ASCII 160 (0xa0) dans le codage ISO 8859-1 par défaut .

+0

Fonctionne, mais pour une raison inconnue, l'encodage html se passe quelque part, pas de mon code. Mais encore, merci! –

0

Essayez d'utiliser cette petite solution:

$str = trim($_POST['text_top']); 
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $str); 

si le poing ne fonctionnait pas bien essayer ceci:

$str = trim($_POST['text_top']); 
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str); 
$clean = preg_replace("/[^a-zA-Z0-9\/_| -]/", '', $clean); 
$clean = strtolower(trim($clean, '-')); 
$clean = preg_replace("/[\/_| -]+/", '-', $clean); 
Questions connexes