[英]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(" ","!",""","#","$","%","&","'","(",")","*","+",",","-",".","/","<","=",">","á","Á","é","É","í","Í","ó","Ó","ö","Ö","ő","Ő","ú","Ú","ü","Ü","ű","Ű","@","[","\","]","^","_","{","|","}","~");
$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)步骤:
Please help me, where did I go wrong? 请帮助我,我哪里出问题了?
There are 2 standard functions available in PHP: PHP提供了2个标准函数:
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 = ' !"#$%&'()*+,-./<=>áÁéÉíÍóÓöÖőŐúÚüÜűŰ@[\]^_{|}~';
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(" ","!",""","#","$","%","&","'","(",")","*","+",",","-",".","/","<","=",">","á","Á","é","É","í","Í","ó","Ó","ö","Ö","ő","Ő","ú","Ú","ü","Ü","ű","Ű","@","[","\","]","^","_","{","|","}","~");
$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(): '!"#$%&'()*+,-./<=>áÁéÉíÍóÓöÖoOúÚüÜuU@[\]^_{|}~
coding(): '!"#$%&'()*+,-./<=>áÁéÉíÍóÓöÖőŐúÚüÜűŰ@[\]^_{|}~
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.