PHP的未定义字符?在输出中 [英] PHP undefined chars ? in output

查看:150
本文介绍了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ä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

tae�p@tecmax.com

tue p@tecmax.com

tue�p@tecmax.com

toe p@tecmax.com

toe�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;
}

这篇关于PHP的未定义字符?在输出中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆