PHP未定义字符?产量
我的问题是,我不知道这些字符在我的输出中的什么位置,有人能解释一下为什么在我的字符串中有这些字符,以及我要做什么来取消设置它们吗 该函数用于将“ä”、“ö”、“ü”更改为“ae”、“oe”、“ue”PHP未定义字符?产量,php,string,ascii,Php,String,Ascii,我的问题是,我不知道这些字符在我的输出中的什么位置,有人能解释一下为什么在我的字符串中有这些字符,以及我要做什么来取消设置它们吗 该函数用于将“ä”、“ö”、“ü”更改为“ae”、“oe”、“ue” <?php // str | string argument // needle | searched char // val | value // pos | default 0 at start at offset zero // pos |
<?php
// str | string argument
// needle | searched char
// val | value
// pos | default 0 at start at offset zero
// pos | momently just working with default offset
function changeLetter($str, $needle, $val, $pos = 0) {
$mstr = "";
while (isset($str[$pos])) {
if (ord($str[$pos]) == ord($needle)) {
$mstr .= $val;
$pos++;
} else {
$mstr .= $str[$pos];
$pos++;
}
}
return $mstr;
}
echo changeLetter("täp@tecmax.com", 'ä', 'ae') . '<br>';
echo changeLetter("tüp@tecmax.com", 'ü', 'ue') . '<br>';
echo changeLetter("töp@tecmax.com", 'ö', 'oe') . '<br>';
//echo changeLetter("täp@tecmax.com", 'ä', 'ae', 3) . '<br>';
?>
输出:
泰伊�p@tecmax.com
星期二�p@tecmax.com
脚趾�p@tecmax.com您需要更改文件编码,或使用
ä代码>,ü代码>,ö
而不是ä
,ü
,ö
,以下是您可以执行的操作:
echo changeLetter("täp@tecmax.com", 'ä', 'ae'), PHP_EOL;
echo changeLetter("tüp@tecmax.com", 'ü', 'ue'), PHP_EOL;
echo changeLetter("töp@tecmax.com", 'ö', 'oe'), PHP_EOL;
输出
taep@tecmax.com
tuep@tecmax.com
toep@tecmax.com
使用的功能
function changeLetter($str, $needle, $val, $pos = 0) {
$next = function ($str, &$pos) {
if (! isset($str[$pos]))
return false;
$char = ord($str[$pos]);
if ($char < 128) {
return $str[$pos ++];
} else {
if ($char < 224) {
$bytes = 2;
} elseif ($char < 240) {
$bytes = 3;
} elseif ($char < 248) {
$bytes = 4;
} elseif ($char = 252) {
$bytes = 5;
} else {
$bytes = 6;
}
$str = substr($str, $pos, $bytes);
$pos += $bytes;
return $str;
}
};
$mstr = "";
while(($chr = $next($str, $pos)) !== false) {
$mstr .= $chr == $needle ? $val : $chr;
}
return $mstr;
}
函数变更函($str、$needle、$val、$pos=0){
$next=函数($str,&$pos){
如果(!isset($str[$pos]))
返回false;
$char=ord($str[$pos]);
如果($char<128){
返回$str[$pos++];
}否则{
如果($char<224){
$bytes=2;
}elseif($char<240){
$bytes=3;
}elseif($char<248){
$bytes=4;
}elseif($char=252){
$bytes=5;
}否则{
$bytes=6;
}
$str=substr($str,$pos,$bytes);
$pos+=$bytes;
返回$str;
}
};
$mstr=“”;
while(($chr=$next($str,$pos))!==false){
$mstr.=$chr==$needle?$val:$chr;
}
返回$mstr;
}
此字符在字符串中使用了多个字节,这就是为什么字符以这种方式显示,请使用str_replace来执行任务。您使用过吗?问题是,如果不允许使用相关的内置函数,我的任务是在没有str_replace/preg_replace etcSo的情况下对此进行编程,听起来像是家庭作业或面试?如果这是一个骗人的问题,那么很可能是为了测试你和你的字符串/编码知识,而你似乎确实缺乏这些知识。→ 不在HTML上下文中时不要使用HTML实体!你真幸运,我今天的票用完了。