将任何字符转换为转义字符

时间:2013-09-18 21:59:32

标签: php

“$”的转义字符是

$

是否有本机php函数可以转换并从任何输入中获取转义字符?

htmlspecialchars正在转换ok但不是所有输入字符串...它不适用于“$”

供参考 - http://www.theukwebdesigncompany.com/articles/entity-escape-characters.php

编辑:

echo htmlentities("$"); //output "$", not "$"

echo htmlspecialchars("$"); //output "$", not "$"

3 个答案:

答案 0 :(得分:0)

在手册用户备注中找到,测试似乎适用于我的所有测试。

<?php
function superentities( $str ){
    // get rid of existing entities else double-escape
    $str = html_entity_decode(stripslashes($str),ENT_QUOTES,'UTF-8');
    $ar = preg_split('/(?<!^)(?!$)/u', $str );  // return array of every multi-byte character
    foreach ($ar as $c){
        $o = ord($c);
        if ( (strlen($c) > 1) || /* multi-byte [unicode] */
            ($o <32 || $o > 126) || /* <- control / latin weirdos -> */
            ($o >33 && $o < 40) ||/* quotes + ambersand */
            ($o >59 && $o < 63) /* html */
        ) {
            // convert to numeric entity
            $c = mb_encode_numericentity($c,array (0x0, 0xffff, 0, 0xffff), 'UTF-8');
        }
        $str2 .= $c;
    }
    return $str2;
}

echo superentities('$'); //&#36;

?>

答案 1 :(得分:-1)

尝试使用php.htmlspecialchars: htmlspecialchars

答案 2 :(得分:-1)

使用htmlentities转换所有可能的字符 - read more

相关问题