【问题标题】:How can I pad each multibyte character / emoji with spaces around it in a string?如何在字符串中用空格填充每个多字节字符/表情符号?
【发布时间】:2017-01-10 21:50:43
【问题描述】:

我想在每个多字节字符的两边都用空格填充。我可以把它们去掉就好了,但我想把它们留在里面,然后把它们垫起来。

例如:???????????????? ???? ????

使用下划线表示空格:????????????_????__????__????_

【问题讨论】:

  • 我可以把它们去掉 - 请分享如何。
  • 当然是维克托。以下是我可以去掉每个多字节字符的方法:preg_replace( "/[^a-z0-9 ]/i", "", $string );。但我不知道如何填充它们。
  • 如果你能找到这个字符,你不能用一个跨度包围它并添加填充/边距吗?
  • 正确。你问我如何去除多字节字符。该评论是对此的回答。我仍然不知道如何在字符串中的每个多字节字符周围填充空格。

标签: php regex emoji multibyte emojione


【解决方案1】:

使用这个怪物-already-cooked regex:

$regex = "[\\x{fe00}-\\x{fe0f}\\x{2712}\\x{2714}\\x{2716}\\x{271d}\\x{2721}\\x{2728}\\x{2733}\\x{2734}\\x{2744}\\x{2747}\\x{274c}\\x{274e}\\x{2753}-\\x{2755}\\x{2757}\\x{2763}\\x{2764}\\x{2795}-\\x{2797}\\x{27a1}\\x{27b0}\\x{27bf}\\x{2934}\\x{2935}\\x{2b05}-\\x{2b07}\\x{2b1b}\\x{2b1c}\\x{2b50}\\x{2b55}\\x{3030}\\x{303d}\\x{1f004}\\x{1f0cf}\\x{1f170}\\x{1f171}\\x{1f17e}\\x{1f17f}\\x{1f18e}\\x{1f191}-\\x{1f19a}\\x{1f201}\\x{1f202}\\x{1f21a}\\x{1f22f}\\x{1f232}-\\x{1f23a}\\x{1f250}\\x{1f251}\\x{1f300}-\\x{1f321}\\x{1f324}-\\x{1f393}\\x{1f396}\\x{1f397}\\x{1f399}-\\x{1f39b}\\x{1f39e}-\\x{1f3f0}\\x{1f3f3}-\\x{1f3f5}\\x{1f3f7}-\\x{1f4fd}\\x{1f4ff}-\\x{1f53d}\\x{1f549}-\\x{1f54e}\\x{1f550}-\\x{1f567}\\x{1f56f}\\x{1f570}\\x{1f573}-\\x{1f579}\\x{1f587}\\x{1f58a}-\\x{1f58d}\\x{1f590}\\x{1f595}\\x{1f596}\\x{1f5a5}\\x{1f5a8}\\x{1f5b1}\\x{1f5b2}\\x{1f5bc}\\x{1f5c2}-\\x{1f5c4}\\x{1f5d1}-\\x{1f5d3}\\x{1f5dc}-\\x{1f5de}\\x{1f5e1}\\x{1f5e3}\\x{1f5ef}\\x{1f5f3}\\x{1f5fa}-\\x{1f64f}\\x{1f680}-\\x{1f6c5}\\x{1f6cb}-\\x{1f6d0}\\x{1f6e0}-\\x{1f6e5}\\x{1f6e9}\\x{1f6eb}\\x{1f6ec}\\x{1f6f0}\\x{1f6f3}\\x{1f910}-\\x{1f918}\\x{1f980}-\\x{1f984}\\x{1f9c0}\\x{3297}\\x{3299}\\x{a9}\\x{ae}\\x{203c}\\x{2049}\\x{2122}\\x{2139}\\x{2194}-\\x{2199}\\x{21a9}\\x{21aa}\\x{231a}\\x{231b}\\x{2328}\\x{2388}\\x{23cf}\\x{23e9}-\\x{23f3}\\x{23f8}-\\x{23fa}\\x{24c2}\\x{25aa}\\x{25ab}\\x{25b6}\\x{25c0}\\x{25fb}-\\x{25fe}\\x{2600}-\\x{2604}\\x{260e}\\x{2611}\\x{2614}\\x{2615}\\x{2618}\\x{261d}\\x{2620}\\x{2622}\\x{2623}\\x{2626}\\x{262a}\\x{262e}\\x{262f}\\x{2638}-\\x{263a}\\x{2648}-\\x{2653}\\x{2660}\\x{2663}\\x{2665}\\x{2666}\\x{2668}\\x{267b}\\x{267f}\\x{2692}-\\x{2694}\\x{2696}\\x{2697}\\x{2699}\\x{269b}\\x{269c}\\x{26a0}\\x{26a1}\\x{26aa}\\x{26ab}\\x{26b0}\\x{26b1}\\x{26bd}\\x{26be}\\x{26c4}\\x{26c5}\\x{26c8}\\x{26ce}\\x{26cf}\\x{26d1}\\x{26d3}\\x{26d4}\\x{26e9}\\x{26ea}\\x{26f0}-\\x{26f5}\\x{26f7}-\\x{26f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\x{1f1fe}\\x{1f1ff}]|\\x{1f1fb}[\\x{1f1e6}\\x{1f1e8}\\x{1f1ea}\\x{1f1ec}\\x{1f1ee}\\x{1f1f3}\\x{1f1fa}]|\\x{1f1fc}[\\x{1f1eb}\\x{1f1f8}]|\\x{1f1fd}\\x{1f1f0}|\\x{1f1fe}[\\x{1f1ea}\\x{1f1f9}]|\\x{1f1ff}[\\x{1f1e6}\\x{1f1f2}\\x{1f1fc}]";

preg_replace_callback() 内:

var_dump(preg_replace_callback("@$regex@u", function($match) {
    return $match[0]."  ";
}, '???'));

输出:

string(18) "?  ?  ?  "

Live demo

【讨论】:

  • 喜欢这个策略。但是每次他们添加新的表情符号时它不会改变吗?
  • 这包括可以定义表情符号的 Unicode 块。
  • 在进一步审查所有答案后。这是最能实现表情符号周围空间目标的方法。其他答案填充了所有非字母数字(例如')。或将mañana 之类的单词拆分为ma ñ ana。因为@revo 能够隔离 emoji unicode 块,所以一切顺利。谢谢!
【解决方案2】:

我发现有人在 PHP 文档中添加了 this function,该文档将多字节字符串拆分为字符数组(如 str_split)并对其进行了修改。

function addSpaces($string) {
    $strlen = mb_strlen($string);
    $new_string = '';
    while ($strlen) {
        $char = mb_substr($string,0,1,"UTF-8");
        if (strlen($char) > 1) {
            $new_string .= " $char ";
        } else {
            $new_string .= $char;
        }
        $string = mb_substr($string,1,$strlen,"UTF-8");
        $strlen = mb_strlen($string);
    }
    return $new_string;
}

This question 有其他方法可以进行类似修改的拆分。修改为,如果其中一个分割字符的strlen大于1,则为多字节,所以加空格。

【讨论】:

  • 这行得通。绕过解决方案的创造性方法,即使需要多行几行才能到达那里。
  • 不客气。使用正则表达式可能有一种更快的方法,但这并不是我的强项。
【解决方案3】:

简单的正则表达式替换也可以工作......

mb_regex_encoding("UTF-8");

echo mb_ereg_replace(
    '([^\p{L}\s])',
    ' \\1 ', 
    'text ??? other text ???'
);

输出:text ? ? ? other text ? ? ?


function pad_emojis($string) {
    $default_encoding = mb_regex_encoding();
    mb_regex_encoding("UTF-8");
    $string = mb_ereg_replace('([^\p{L}\s])', ' \\1 ', $string);
    mb_regex_encoding($default_encoding);
    return $string;
}

【讨论】:

  • 为简单点赞。这可能是我最喜欢的。现在测试一下。
  • 已确认。像宣传的那样工作。非常感谢。
  • 是的,它将在更多字符上应用填充,例如标点符号。 @瑞安
  • 嗯.. @revo 是对的.. 根据您的使用情况,您还可以像这样删除整个范围:mb_ereg_replace('([^\x{0000}-\x{FFFF}])', ... 所以您确定您只匹配多字节字符。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-08
  • 1970-01-01
  • 1970-01-01
  • 2015-09-18
  • 2012-08-06
  • 1970-01-01
  • 2023-03-30
相关资源
最近更新 更多