如何使用PHP将所有字符转换为等效的html实体

时间:2021-07-10 00:25:59

I want to convert this hello@domain.com to

我想将此hello@domain.com转换为

hello@domain.com

I have tried:

我努力了:

url_encode($string)

this provides the same string I entered, returned with the @ symbol converted to %40

这提供了我输入的相同字符串,返回时@符号转换为%40

also tried:

htmlentities($string)

this provides the same string right back.

这提供了相同的字符串。

I am using a UTF8 charset. not sure if this makes a difference....

我使用的是UTF8字符集。不确定这是否有所作为....

3 个解决方案

#1


40  

Here it goes (assumes UTF-8, but it's trivial to change):

在这里它(假设UTF-8,但改变是微不足道的):

function encode($str) {
    $str = mb_convert_encoding($str , 'UTF-32', 'UTF-8'); //big endian
    $split = str_split($str, 4);

    $res = "";
    foreach ($split as $c) {
        $cur = 0;
        for ($i = 0; $i < 4; $i++) {
            $cur |= ord($c[$i]) << (8*(3 - $i));
        }
        $res .= "&#" . $cur . ";";
    }
    return $res;
}

EDIT Recommended alternative using unpack:

编辑使用解包的推荐替代方案:

function encode2($str) {
    $str = mb_convert_encoding($str , 'UTF-32', 'UTF-8');
    $t = unpack("N*", $str);
    $t = array_map(function($n) { return "&#$n;"; }, $t);
    return implode("", $t);
}

#2


8  

Much easier way to do this:

更简单的方法:

function convertToNumericEntities($string) {
    $convmap = array(0x80, 0x10ffff, 0, 0xffffff);
    return mb_encode_numericentity($string, $convmap, "UTF-8");
}

You can change the encoding if you are using anything different.

如果您使用的是其他任何内容,则可以更改编码。

  • Fixed map range. Thanks to Artefacto.
  • 固定地图范围。感谢Artefacto。

#3


1  

function uniord($char) {

     $k=mb_convert_encoding($char , 'UTF-32', 'UTF-8');

     $k1=ord(substr($k,0,1));

     $k2=ord(substr($k,1,1));

     $value=(string)($k2*256+$k1);

     return $value;

}

the above function works for 1 character but if you have a string you can do like this

上面的函数适用于1个字符,但如果你有一个字符串,你可以这样做

$string="anytext";

$arr=preg_split(//u,$string,-1,PREG_SPLIT_NO_EMPTY);

$temp=" ";

foreach($arr as $v){

    $temp="&#".uniord($v);//prints the equivalent html entity of string

}

#1


40  

Here it goes (assumes UTF-8, but it's trivial to change):

在这里它(假设UTF-8,但改变是微不足道的):

function encode($str) {
    $str = mb_convert_encoding($str , 'UTF-32', 'UTF-8'); //big endian
    $split = str_split($str, 4);

    $res = "";
    foreach ($split as $c) {
        $cur = 0;
        for ($i = 0; $i < 4; $i++) {
            $cur |= ord($c[$i]) << (8*(3 - $i));
        }
        $res .= "&#" . $cur . ";";
    }
    return $res;
}

EDIT Recommended alternative using unpack:

编辑使用解包的推荐替代方案:

function encode2($str) {
    $str = mb_convert_encoding($str , 'UTF-32', 'UTF-8');
    $t = unpack("N*", $str);
    $t = array_map(function($n) { return "&#$n;"; }, $t);
    return implode("", $t);
}

#2


8  

Much easier way to do this:

更简单的方法:

function convertToNumericEntities($string) {
    $convmap = array(0x80, 0x10ffff, 0, 0xffffff);
    return mb_encode_numericentity($string, $convmap, "UTF-8");
}

You can change the encoding if you are using anything different.

如果您使用的是其他任何内容,则可以更改编码。

  • Fixed map range. Thanks to Artefacto.
  • 固定地图范围。感谢Artefacto。

#3


1  

function uniord($char) {

     $k=mb_convert_encoding($char , 'UTF-32', 'UTF-8');

     $k1=ord(substr($k,0,1));

     $k2=ord(substr($k,1,1));

     $value=(string)($k2*256+$k1);

     return $value;

}

the above function works for 1 character but if you have a string you can do like this

上面的函数适用于1个字符,但如果你有一个字符串,你可以这样做

$string="anytext";

$arr=preg_split(//u,$string,-1,PREG_SPLIT_NO_EMPTY);

$temp=" ";

foreach($arr as $v){

    $temp="&#".uniord($v);//prints the equivalent html entity of string

}