【发布时间】:2013-05-23 16:02:18
【问题描述】:
我的问题是我不知道我的输出中的那些字符是从哪里来的,谁能解释我为什么在我的字符串中是这些字符以及我该怎么做才能取消它们?
该函数用于将'ä'、'ö'、'ü'变为'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ä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>';
?>
输出:
tae�p@tecmax.com
tue�p@tecmax.com
toe�p@tecmax.com
【问题讨论】:
-
这个字符在字符串中使用了多个字节,这就是字符以这种方式显示的原因,使用 str_replace 来完成你的任务。
-
你用过
str_replace吗? -
问题是我的任务是在没有 str_replace / preg_replace 等的情况下对此进行编程
-
那么,如果你不允许使用相关的内置功能,听起来像是作业或面试?
-
如果这是一个技巧问题,那么它可能是为了测试您和您的字符串/编码知识,您似乎确实缺乏这些知识。 →What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text