使用PHP以纯文本格式检测表情符号

问题描述 投票:0回答:2

我想以下列形式更改表达式:

[E-01] - [E-02] - [E-03] - [E-04] ... 

<img src = "E-XX.png">

我怎样才能做到这一点?

Input

$message = "Hello [E-02] World [E-01]";

Desired Output

Hello <img src="E-02.png"> World <img src="E-01.png">
php preg-match preg-match-all
2个回答
1
投票

Preg_replace是你的朋友:

$message = "Hello [E-02] World [E-01]";
$replaced = preg_replace('/\[(E-\d\d)\]/', '<img src="/assets/images/emoji/$1.png" height="20" width="20" />', $message);
echo $replaced;

输出:

Hello <img src="/assets/images/emoji/E-02.png" height="20" width="20" /> World <img src="/assets/images/emoji/E-01.png" height="20" width="20" />

0
投票
function emoji($message){
            for($e=1; $e <= 60; $e++){
                $e    = (($e < 10) ? '0' . $e : $e);
                $name = 'E-' . $e;
                $message = str_replace('[' . $name . ']', ' <img src="/assets/images/emoji/' . $name. '.png" height="20" width="20" /> ', $message);
            }
            return $message;
        }
© www.soinside.com 2019 - 2024. All rights reserved.