本文介绍了PHP的未定义字符?在输出中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题是我不知道输出中的那些字符,谁能解释我为什么这些字符在我的字符串中以及为什么要取消设置它们?
My problem is that i dont know from where those chars in my output, can anyone explain me why in my string are those characters and what ive to do to unset them ?
该函数用于将ä",ö",ü"更改为"ae","oe","ue"
the function is used to change 'ä', 'ö', 'ü' into 'ae', 'oe', 'ue'
<?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ä[email protected]", 'ä', 'ae') . '<br>';
echo changeLetter("tü[email protected]", 'ü', 'ue') . '<br>';
echo changeLetter("tö[email protected]", 'ö', 'oe') . '<br>';
//echo changeLetter("tä[email protected]", 'ä', 'ae', 3) . '<br>';
?>
输出:
推荐答案
您可以执行以下操作:
echo changeLetter("tä[email protected]", 'ä', 'ae'), PHP_EOL;
echo changeLetter("tü[email protected]", 'ü', 'ue'), PHP_EOL;
echo changeLetter("tö[email protected]", 'ö', 'oe'), PHP_EOL;
输出
[email protected]
[email protected]
[email protected]
使用的功能
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;
}
这篇关于PHP的未定义字符?在输出中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!