【问题标题】:Getting PHP code to recognize special characters让 PHP 代码识别特殊字符
【发布时间】: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


【解决方案1】:

使用

&amp;#35; 你想要一个#

&amp;#38; 你想要一个 &

&amp;#43; 你想要一个 +

&amp;#92; 你想要一个 \

【讨论】:

    【解决方案2】:

    您可以使用以下PHP进行转换:

    $html = '&#'.ord('#').';';
    

    ord 函数将字符转换为 ASCII 代码(数字)。您可以在此处找到 ASCII 字符列表及其数值:http://www.asciitable.com/

    转换所有特殊字符的函数:

    function convertSpecialToHTML($char) {
        $special = ['#', '&', '+', '\\'];
    
        if (in_array($char, $special)) {
            return '&#'.ord($char).';';
        } else {
            return $char;
        }
    }
    

    演示: http://ideone.com/vGNZ5e

    你也应该看看htmlentities

    echo htmlentities("I'm a string with all your special characters: \ + & #", ENT_HTML5);
    //I'm a string with all your special characters&colon; &bsol; &plus; &amp; &num;
    

    【讨论】:

    • 好的,我试过了,但屏幕的输出显示为 HTML 解码格式。我需要正确显示字符。我将把我的代码上传到我原来的问题中,这样你就可以看到它在做什么。
    • 你也试过convertSpecialToHTML函数??
    • 不,那个我没试过,我下次试试。
    • 好的,我都试过了,这些都没有向浏览器输出任何东西。你们中的哪一个能帮我写出这部分吗?
    • 有没有人知道在这种情况下我需要做什么?
    【解决方案3】:

    特别感谢用户 @dorad 帮助我使其正常工作。这是实际工作的代码。我怀疑我的原始版本是为早期版本的 PHP 编码的,在较新的 PHP 版本中对于特殊字符无法正常工作。因此,事不宜迟,这是最终有效的代码:

    <?php
    //creates a image handle
    $image = imagecreate( 700, 70 );
    
    if(!empty($_POST["bgcolor"])){
    $background = imagecolorallocate( $image,0, 0, 0);
    }
    
    else {
    $background = imagecolorallocate( $image,255, 255, 255);
    }
    
    //GET COLORS FROM POST AND SPLIT INTO RGB FORMAT
    $color  =  $_POST["color"];
    $pieces = explode("-", $color);
     //COLORS
    $color = imagecolorallocate($image, hexdec("0x".$pieces[0].""), hexdec("0x".$pieces[1].""),  hexdec("0x".$pieces[2].""));
    
    
    $font = 'fonts/'.$_POST["font"].'';
    $fontSize = "25";
    $fontRotation = "0";
    $str = utf8_encode_mix($_POST["name"]);
    
    $regex = $str;
    $replaced = preg_replace($regex,"%26",$str);
    
    
    $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;
        }
    ?>
    

    调用页面index2.php

    <div style="padding:00px;">
    
    <strong>Please complete this form to preview the font and color font your name Embroidery: </strong>
    <br><br>
    
    
    <form action="index2.php?page=embroidery" method="post" name="font" style="background-color:#EDFAFC;">
    
    <input name="page" type="hidden" value="lab_coats_embroidery">
    <?
    function checkcolor ($color) {
    
    if($color == $_POST["color"]){
        echo "checked";
    }
    }
    
    //
    ?>
    
    
    <table width="600" border="0" cellspacing="4" cellpadding="4">
      <tr>
        <td width="163">Please enter your name: </td>
        <td width="398">Line 1:
          <input name="name" type="text" value="<?=$_POST["name"]?>" size="40">
          <br />
          Line 2:
          <input name="name2" type="text" value="<?=$_POST["name2"]?>" size="40" />
    <br />
          Line 3:
          <input name="name3" type="text" value="<?=$_POST["name3"]?>" size="40" /></td>
        <td width="18">&nbsp;</td>
        <td width="21">&nbsp;</td>
      </tr>
      <tr>
        <td>Please select a font: </td>
        <td><select name="font" id="font">
         <option value="<?=$_POST["font"]?>"></option>
          <option value="brush">Brush script size 14</option>
          <option value="athletic">Athletic swoosh</option>
          <!--<option value="diana">Diana script</option>-->
          <option value="brush738">Brush 738</option>
          <option value="helvetica">Helvetica narrow</option>
          <option value="homeward">Homeward</option>
          <option value="cheltenham">Cheltenham</option>
          <option value="athletic_narrow">Athletic Narrow</option>
          <option value="courier">Courier</option>
    <?php /*?>      <option value="cancun">Cancun</option><?php */?>
        </select>
          Color:
          <select name="color" id="color">
            <option value="<?=$_POST["color"]?>"></option>
            <option value="00-00-00">Black</option>
            <option value="cc-00-00">Red</option>
            <option value="05-34-92">Royal Blue</option>
            <option value="13-2c-61">Navy</option>
            <option value="86-00-41">Burgundy</option>
            <option value="fd-08-c8">Fuchsia</option>
            <option value="73-b9-ff">Sky Blue</option>
            <option value="02-74-8c">Teal</option>
            <option value="02-4b-2d">Forest Green</option>
            <option value="43-07-71">Purple</option>
            <option value="ff-c6-00">Gold</option>
            <option value="dd-dd-dd">Silver</option>
            <option value="96-54-31">Bronze</option>
          </select></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td><input type="submit" name="button" id="button" value="Preview My Name" /></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    </form>
    
    <div style="text-align:center;">
    <!-- orig setting height 65px and no background positioning -->
    <div style="background-image:url('dpimage.php?name=<?=$_POST["name"]?>&font=<?=$_POST["font"]?>&color=<?=$_POST["color"]?>'); background-repeat:no; background-position: center -20px; width:700px; height:40px;"></div>
    <div style="background-image:url('dpimage.php?name=<?=$_POST["name2"]?>&font=<?=$_POST["font"]?>&color=<?=$_POST["color"]?>'); background-repeat:no; background-position: center -20px; width:700px; height:40px;"></div>
    <div style="background-image:url('dpimage.php?name=<?=$_POST["name3"]?>&font=<?=$_POST["font"]?>&color=<?=$_POST["color"]?> '); background-repeat:no; background-position: center -20px; width:700px; height:40px;"></div>
    
    </div>
    
    </div>
    

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-25
      • 1970-01-01
      • 2013-03-22
      • 1970-01-01
      相关资源
      最近更新 更多