切换条件

时间:2017-07-28 15:07:35

标签: php

在此代码中我正在检查与我的数组键匹配的用户输入值我是否要将defualt纯值放在开关条件的默认部分但它回显empety值为什么switch默认不工作什么是我的错误 三江源

我为if条件添加了一个else语句但是多次返回valuse我不想要这个

 const EMOJI = [
        ":lol" => "emoji/0046.gif",
        ":d" => "emoji/0055.gif",
        ":|" => "emoji/poker.png",
        ":(" => "emoji/0005.gif",
        ":cool" => "emoji/0038.gif",
        ":shy" => "emoji/0001.gif",
        ":)" => "emoji/0003.gif",
        ":p" => "emoji/0008.gif",
        ":h" => "emoji/0031.gif",
        ":search" => "emoji/0042.gif",
        ":'(" => "emoji/0057.gif",
        ":cry" => "emoji/0048.gif",
        ":think" => "emoji/0052.gif",
        ":y" => "emoji/0053.gif",
        ":@" => "emoji/0052.gif",
        ":xd" => "emoji/0056.gif",
        ":punch" => "emoji/0063.gif",
        ":*" => "emoji/0064.gif",
        ":angle" => "emoji/0065.gif",
        ":sick" => "emoji/0013.gif",
        ":flower" => "emoji/0067.gif"
    ];


    if ($_POST['method'] === 'fetch') {
        $messages = $chat->fetchMessage();

        if (!empty($messages)) {
            foreach ($messages as $key => $value) { ?>

            <div class="message" id="<?php echo $value['message_id'] ?>">
                <p class="username"><?php echo $value['username']; ?></p><span style="font-size: 12px">say:</span>
                <?php


                $value = strtolower($value['message']);
                $arraykey = array_keys(EMOJI);
                for ($i = 0; $i < count($arraykey); $i++) {
                    if (preg_match_all("~" . preg_quote($arraykey[$i], '~') . "~", $value, $match)) {
                        switch ($match[0][0]) {
                            case ":lol":
                                $one = str_replace($match[0], '<img src=" '.EMOJI[":lol"].'"', $value);
                                echo $one;
                                break;
                            case ":d":
                                $one = str_replace($match[0], '<img src=" '.EMOJI[":d"].'"', $value);
                                echo $one;
                                break;
                            case ":|":
                                $one = str_replace($match[0], '<img src=" '.EMOJI[":|"].'"', $value);
                                echo $one;
                                break;
                            case ":(":
                                $one = str_replace($match[0], '<img src=" '.EMOJI[":("].'"', $value);
                                echo $one;
                                break;
                            case ":cool":
                                $one = str_replace($match[0], '<img src=" '.EMOJI[":cool"].'"', $value);
                                echo $one;
                                break;
                            case ":shy":
                                $one = str_replace($match[0], '<img src=" '.EMOJI[":shy"].'"', $value);
                                echo $one;
                                break;
                            case ":)":
                                $one = str_replace($match[0], '<img src=" '.EMOJI[":)"].'"', $value);
                                echo $one;
                                break;
                            case ":P":
                                $one = str_replace($match[0], '<img src=" '.EMOJI[":p"].'"', $value);
                                echo $one;
                                break;
                            case ":h":
                                $one = str_replace($match[0], '<img src=" '.EMOJI[":h"].'"', $value);
                                echo $one;
                                break;
                            case ":search":
                                $one = str_replace($match[0], '<img src=" '.EMOJI[":search"].'"', $value);
                                echo $one;
                                break;
                            case ":'(":
                                $one = str_replace($match[0], '<img src=" '.EMOJI[":'("].'"', $value);
                                echo $one;
                                break;
                            case ":cry":
                                $one = str_replace($match[0], '<img src=" '.EMOJI[":cry"].'"', $value);
                                echo $one;
                                break;
                            case ":think":
                                $one = str_replace($match[0], '<img src=" '.EMOJI[":think"].'"', $value);
                                echo $one;
                                break;
                            case ":y":
                                $one = str_replace($match[0], '<img src=" '.EMOJI[":y"].'"', $value);
                                echo $one;
                                break;
                            case ":@":
                                $one = str_replace($match[0], '<img src=" '.EMOJI[":@"].'"', $value);
                                echo $one;
                                break;
                            case ":xd":
                                $one = str_replace($match[0], '<img src=" '.EMOJI[":xd"].'"', $value);
                                echo $one;
                                break;
                            case ":punch":
                                $one = str_replace($match[0], '<img src=" '.EMOJI[":punch"].'"', $value);
                                echo $one;
                                break;
                            case ":*":
                                $one = str_replace($match[0], '<img src=" '.EMOJI[":*"].'"', $value);
                                echo $one;
                                break;
                            case ":angle":
                                $one = str_replace($match[0], '<img src=" '.EMOJI[":angle"].'"', $value);
                                echo $one;
                                break;
                            case ":sick":
                                $one = str_replace($match[0], '<img src=" '.EMOJI[":sick"].'"', $value);
                                echo $one;
                                break;
                            case ":flower":
                                $one = str_replace($match[0], '<img src=" '.EMOJI[":flower"].'"', $value);
                                echo $one;
                                break;
                            default:
                                echo $value;
                        }

                    } ?>
                    </div>
                    <?php
                }
            }
        } else {
            echo "empety message";
        }

    } 

1 个答案:

答案 0 :(得分:2)

您尝试实现的许多简化版本:

const EMOJI = [
    ":lol" => "emoji/0046.gif",
    ":d" => "emoji/0055.gif",
    ":|" => "emoji/poker.png",
    ":(" => "emoji/0005.gif",
    ":cool" => "emoji/0038.gif",
    ":shy" => "emoji/0001.gif",
    ":)" => "emoji/0003.gif",
    ":p" => "emoji/0008.gif",
    ":h" => "emoji/0031.gif",
    ":search" => "emoji/0042.gif",
    ":'(" => "emoji/0057.gif",
    ":cry" => "emoji/0048.gif",
    ":think" => "emoji/0052.gif",
    ":y" => "emoji/0053.gif",
    ":@" => "emoji/0052.gif",
    ":xd" => "emoji/0056.gif",
    ":punch" => "emoji/0063.gif",
    ":*" => "emoji/0064.gif",
    ":angle" => "emoji/0065.gif",
    ":sick" => "emoji/0013.gif",
    ":flower" => "emoji/0067.gif"
];

// Here we get keys of EMOJI array
$keys = array_keys(EMOJI);
// Escape special symbols in each key
foreach ($keys as &$k) {
    $k = str_replace(['|', '(', ')', '*'], ['\|', '\(', '\)', '\*'], $k);
}

// Test string
$str = ':flower :@ Yeah :( :*';
// Pattern
$pattern = '~(' . implode(')|(', $keys) . ')~';

// Replacing
$r = preg_replace_callback(
    $pattern,
    function($m) {
        return EMOJI[$m[0]];
        // Wrap in `img` tag
        // return '<img src="' . EMOJI[$m[0]] . '">';
    },
    $str
);

// Output `emoji/0067.gif emoji/0052.gif Yeah emoji/0005.gif emoji/0064.gif`
echo $r;    

supershort 版本是:

const EMOJI = [
    ":lol" => "emoji/0046.gif",
    ":d" => "emoji/0055.gif",
    ":|" => "emoji/poker.png",
    ":(" => "emoji/0005.gif",
    ":cool" => "emoji/0038.gif",
    ":shy" => "emoji/0001.gif",
    ":)" => "emoji/0003.gif",
    ":p" => "emoji/0008.gif",
    ":h" => "emoji/0031.gif",
    ":search" => "emoji/0042.gif",
    ":'(" => "emoji/0057.gif",
    ":cry" => "emoji/0048.gif",
    ":think" => "emoji/0052.gif",
    ":y" => "emoji/0053.gif",
    ":@" => "emoji/0052.gif",
    ":xd" => "emoji/0056.gif",
    ":punch" => "emoji/0063.gif",
    ":*" => "emoji/0064.gif",
    ":angle" => "emoji/0065.gif",
    ":sick" => "emoji/0013.gif",
    ":flower" => "emoji/0067.gif"
];
$str = ':flower :@ Yeah :( :*';
$r = strtr($str, EMOJI);
// Output `emoji/0067.gif emoji/0052.gif Yeah emoji/0005.gif emoji/0064.gif`
echo $r;