duan198409
2017-11-27 05:58
浏览 126
已采纳

PHP substr打破表情符号

I need to get only 30 characters from the paragraph submitted by user. In case the 30th character is an emoji, the output shows question marks. How can I avoid breaking the emojis?

echo substr("Hello world Hello world Hell

图片转代码服务由CSDN问答提供 功能建议

我需要从用户提交的段落中只获取30个字符。 如果第30个字符是表情符号,则输出显示问号。 我怎样才能避免打破表情符号?

echo substr(“Hello world HelloworldHell

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongzhuner6981 2017-11-27 06:11
    已采纳

    I think the simplest solution would be to use mb_substr

    Performs a multi-byte safe substr() operation based on number of characters.

    php > $myvariable = array();
    php > $myvariable['hello'] = mb_substr("Hello world Hello world Hell
    已采纳该答案
    打赏 评论
  • dongyu7074 2017-11-27 06:04


     <meta charset="ISO-8859-1"> 
    
    

    OR

    function entities( $string ) {
    $stringBuilder = "";
    $offset = 0;

    if ( empty( $string ) ) {
        return "";
    }
    
    while ( $offset &gt;= 0 ) {
        $decValue = ordutf8( $string, $offset );
        $char = unichr($decValue);
    
        $htmlEntited = htmlentities( $char );
        if( $char != $htmlEntited ){
            $stringBuilder .= $htmlEntited;
        } elseif( $decValue &gt;= 128 ){
            $stringBuilder .= "&amp;#" . $decValue . ";";
        } else {
            $stringBuilder .= $char;
        }
    }
    
    return $stringBuilder;
    

    }

    // source - http://php.net/manual/en/function.ord.php#109812
    function ordutf8($string, &$offset) {
    $code = ord(substr($string, $offset,1));
    if ($code >= 128) { //otherwise 0xxxxxxx
    if ($code < 224) $bytesnumber = 2; //110xxxxx
    else if ($code < 240) $bytesnumber = 3; //1110xxxx
    else if ($code < 248) $bytesnumber = 4; //11110xxx
    $codetemp = $code - 192 - ($bytesnumber > 2 ? 32 : 0) - ($bytesnumber > 3 ? 16 : 0);
    for ($i = 2; $i <= $bytesnumber; $i++) {
    $offset ++;
    $code2 = ord(substr($string, $offset, 1)) - 128; //10xxxxxx
    $codetemp = $codetemp*64 + $code2;
    }
    $code = $codetemp;
    }
    $offset += 1;
    if ($offset >= strlen($string)) $offset = -1;
    return $code;
    }

    // source - http://php.net/manual/en/function.chr.php#88611
    function unichr($u) {
    return mb_convert_encoding('&#' . intval($u) . ';', 'UTF-8', 'HTML-ENTITIES');
    }

    /* ---- */

    var_dump( entities( "&" ) ) . "
    ";
    var_dump( entities( "<" ) ) . "
    ";
    var_dump( entities( "

    打赏 评论
  • douxu4610 2017-11-27 06:07
    $first = preg_replace_callback('/[\x{80}-\x{10FFFF}]/u', function ($m) {
        $char = current($m);
        $utf = iconv('UTF-8', 'UCS-4', $char);
        return sprintf("&#x%s;", ltrim(strtoupper(bin2hex($utf)), "0"));
    }, $string);
    

    Output

    string 'Fran&#xE7;ais' (length=13)
    

    OR

    echo json_decode('"\uD83D\uDE00"');
    
    打赏 评论

相关推荐 更多相似问题