【发布时间】:2017-05-15 12:43:38
【问题描述】:
我有这个文本模拟器,它允许访问者选择他们想要的特定字体和他们想要的行数。此功能类似于在 ThingsRemembered.com 上进行预览,因此您可以在购买之前查看该项目的外观。它运行良好,但是某些特殊字符在模拟器中不起作用。它们如下:
# - Does not appear
& - truncates this and anything after it
+ - Does not appear
\ - Does not appear
' - Erases entire line
我想我需要转义这些字符并替换为它们的 HTML 友好等价物;有没有人有一个如何做到这一点的例子?
<?php
//creates a image handle
$image = imagecreate( 700, 70 );
if(!empty($_GET["bgcolor"])){
$background = imagecolorallocate( $image,0, 0, 0);
}
else {
$background = imagecolorallocate( $image,255, 255, 255);
}
//GET COLORS FROM POST AND SPLIT INTO RGB FORMAT
$color = $_GET["color"];
$pieces = explode("-", $color);
//COLORS
$color = imagecolorallocate($image, hexdec("0x".$pieces[0].""), hexdec("0x".$pieces[1].""), hexdec("0x".$pieces[2].""));
$font = 'fonts/'.$_GET["font"].'';
$fontSize = "25";
$fontRotation = "0";
$str = utf8_encode_mix($_GET["name"]);
// char code replacements
$tb = imagettfbbox(25, 0, $font, $str);
$x = ceil((700 - $tb[2]) / 2);
ImageTTFText($image, $fontSize, $fontRotation, $x, 50, $color, $font, $str);
header("Content-Type: image/PNG");
ImagePng ($image);
imagedestroy($image);
function utf8_encode_mix($input, $encode_keys=false)
{
if(is_array($input))
{
$result = array();
foreach($input as $k => $v)
{
$key = ($encode_keys)? utf8_encode($k) : $k;
$result[$key] = utf8_encode_mix( $v, $encode_keys);
}
}
else
{
$result = utf8_encode($input);
}
return $result;
}
?>
通过将来自调用 PHP 页面的文本传递到此 PHP 代码中来解析用户的输入。输入字符串作为查询字符串变量传递给该例程;这不是我的设计,而是我继承的东西。
【问题讨论】:
-
什么是“文本模拟器”?
-
您打算通过帖子使用此内容进行实时编辑吗?还是您更像在markdown 系统上工作?在 live 系统中,最好考虑 JS 脚本之类的。如果您将以一种方式编译,那么替换为 HTML 应该没问题。
-
文本模拟器的意思类似于您在使用他们的雕刻模拟器时在 thingsremembered.com 网站上看到的内容,因此您可以预览购买的商品的外观。
-
所谓的“文本模拟器”如何解析用户输入?这是您的问题中缺少的部分,也可能是回答您问题的关键。
标签: php special-characters truncated