donglu1971 2012-10-11 20:06
浏览 56
已采纳

strip_tags和trim无法正常工作

I am making a meme generator, Imagick generating the images. My problem is that even if I do some operations on the string I use for the image, the output is incorrect.

By example:

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

Then I do:

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

But on the display of $text_top, after I paste the variable on the image, I get:

&nbsptest test&lt;br&gt;

Why does it happen if I'm calling strip_tags and trim, from what I see, correct and as usual?

All is UTF8 encoded.

Thank!

EDIT: (full code)

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条回答 默认 最新

  • dsfdfd1211 2012-10-11 20:11
    关注

    you could try this?

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

    It seems like your string is html encoded.

    EDIT

    Added support for UTF-8 encoding. That way, the non-breaking space gets trimmed properly, instead of giving a ?.

    From PHP's html_entity_decode documentation:

    Note:

    You might wonder why trim(html_entity_decode(' ')); doesn't reduce the string to an empty string, that's because the ' ' entity is not ASCII code 32 (which is stripped by trim()) but ASCII code 160 (0xa0) in the default ISO 8859-1 encoding.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟