简体   繁体   English

用php脚本替换特殊字符

[英]Replacing special characters with php script

I want to replace the special characters with html codes. 我想用html代码替换特殊字符。 Here is the code, but it return with: 0 . 这是代码,但是返回: 0

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Coding...</title>
</head>
<body>
<?php
function coding($string)
    {
        $character = array(" ","!",'"',"#","$","%","&","'","(",")","*","+",",","-",".","/","<","=",">","á","Á","é","É","í","Í","ó","Ó","ö","Ö","ő","Ő","ú","Ú","ü","Ü","ű","Ű","@","[","\\","]","^","_","{","|","}","~");
        $code = array("&#32;","&#33;","&#34;","&#35;","&#36;","&#37;","&#38;","&#39;","&#40;","&#41;","&#42;","&#43;","&#44;","&#45;","&#46;","&#47;","&#60;","&#61;","&#62;","&#225;","&#193;","&#233;","&#201;","&#237;","&#205;","&#243;","&#211;","&#246;","&#214;","&#337;","&#336;","&#250;","&#218;","&#252;","&#220;","&#369;","&#368;","&#64;","&#91;","&#92;","&#93;","&#94;","&#95;","&#123;","&#124;","&#125;","&#126;");
        $length=count($character);
        $string1=array();
        for ($a = 0; $a<strlen($string); $a++)
            {
                $found=false;
                $sz='';
                $char=$string[$a];
                for ($i = 0; $i<=count($character); $i++) 
                    {
                        if ($character[$i]==$string[$a])
                            {
                                $sz= $code[$i];
                                $i=count($character);
                                $found=true;
                            };
                    };
                if (!$found) {$sz= $char;}
                //echo $sz;
                $string1[]=$sz; 
            };
        for ($a = 0; $a<=count($string1); $a++)
            {
                $string2=$string2+$string1[$a];
            };
        return $string2;
    };
$string = "áÁéÉíÍóÓöÖőŐúÚüÜűŰ !";
echo coding($string)."\n";
?>
</body>
</html>

coding($string) steps: 编码($ string)步骤:

  • create two arrays 创建两个数组
  • in the for cycle it one by one check the characters, if it special add html code to $string1 array else add the original character for循环中,它一个接一个地检查字符,如果特殊,则将html代码添加到$ string1数组中,否则添加原始字符
  • create string ($string2) from the array 从数组创建字符串($ string2)

Please help me, where did I go wrong? 请帮助我,我哪里出问题了?

There are 2 standard functions available in PHP: PHP提供了2个标准函数:

  • htmlspecialchars() : transform normal into special characters. htmlspecialchars() :将normal转换为特殊字符。
  • htmlspecialchars_decode() : transfrom special into normal characters. htmlspecialchars_decode() :从特殊字符转换为普通字符。

Based on your code you find a function that transfers from normal to special (encoded) characters. 根据您的代码,您可以找到一个从普通字符转换为特殊(编码)字符的函数。 This is probably what you tried to do. 这可能是您尝试执行的操作。

    <?php
    // Standard PHP functions
    $string = '&#32;&#33;&#34;&#35;&#36;&#37;&#38;&#39;&#40;&#41;&#42;&#43;&#44;&#45;&#46;&#47;&#60;&#61;&#62;&#225;&#193;&#233;&#201;&#237;&#205;&#243;&#211;&#246;&#214;&#337;&#336;&#250;&#218;&#252;&#220;&#369;&#368;&#64;&#91;&#92;&#93;&#94;&#95;&#123;&#124;&#125;&#126;';
    echo 'htmlspecialchars_decode(): ' . htmlspecialchars_decode($string, ENT_NOQUOTES) . '<br />';
    $string = "'!\"#$%&'()*+,-./<=>áÁéÉíÍóÓöÖoOúÚüÜuU@[\]^_{|}~";
    echo 'htmlspecialchars(): ' . htmlentities(htmlspecialchars($string, ENT_COMPAT)) . '<br />';

    // Based on your code
    function coding($string)
    {
        $character = array(" ","!",'"',"#","$","%","&","'","(",")","*","+",",","-",".","/","<","=",">","á","Á","é","É","í","Í","ó","Ó","ö","Ö","o","O","ú","Ú","ü","Ü","u","U","@","[","\\","]","^","_","{","|","}","~");
        $code = array("&#32;","&#33;","&#34;","&#35;","&#36;","&#37;","&#38;","&#39;","&#40;","&#41;","&#42;","&#43;","&#44;","&#45;","&#46;","&#47;","&#60;","&#61;","&#62;","&#225;","&#193;","&#233;","&#201;","&#237;","&#205;","&#243;","&#211;","&#246;","&#214;","&#337;","&#336;","&#250;","&#218;","&#252;","&#220;","&#369;","&#368;","&#64;","&#91;","&#92;","&#93;","&#94;","&#95;","&#123;","&#124;","&#125;","&#126;");
        $result = '';
        $maxind = strlen($string);
        for($i=0; $i<$maxind; $i++) {
            $index = array_search($string[$i], $character);
            if ($index!==false) $result .= $code[$index];
        }
        return $result;
    };
    $string = "'!\"#$%&'()*+,-./<=>áÁéÉíÍóÓöÖoOúÚüÜuU@[\]^_{|}~";
    echo 'coding(): ' . htmlentities(coding($string)) . '<br />'; // htmlentities() to show "as is" in a html page

...gives this: ...给出:

htmlspecialchars_decode(): !"#$%&'()*+,-./<=>áÁéÉíÍóÓöÖőŐúÚüÜűŰ@[\]^_{|}~
htmlspecialchars(): '!&quot;#$%&amp;'()*+,-./&lt;=&gt;áÁéÉíÍóÓöÖoOúÚüÜuU@[\]^_{|}~
coding(): &#39;&#33;&#34;&#35;&#36;&#37;&#38;&#39;&#40;&#41;&#42;&#43;&#44;&#45;&#46;&#47;&#60;&#61;&#62;&#225;&#193;&#233;&#201;&#237;&#205;&#243;&#211;&#246;&#214;&#337;&#336;&#250;&#218;&#252;&#220;&#369;&#368;&#64;&#91;&#92;&#93;&#94;&#95;&#123;&#124;&#125;&#126;

If do not talk about real problem: 如果不谈论真正的问题:

1) You not initialize $string2 variable 1)您没有初始化$string2变量

2) Invalid condition in loops: 2)循环中的无效条件:

for ($i = 0; $i<=count($character); $i++) 

must be 一定是

for ($i = 0; $i<count($character); $i++)

and

for ($a = 0; $a<=count($string1); $a++)

must be 一定是

for ($a = 0; $a<count($string1); $a++)

3) You do not concatenate the strings, you are convert them into numbers and summing up their: 3)您不连接字符串,而是将它们转换为数字并求和:

$string2=$string2+$string1[$a];

must be 一定是

$string2 = $string2 . $string1[$a];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM