1

I have test code

$chatText = "Hello world :D";

$chatText = str_replace(":D","<img src='happy.jpg' width='20' height='20' alt='Big Smile' />",$chatText);

echo $chatText;

basically what it does is replace the :D with an image. What I actually wanted to do is after clicking submit from a <textarea> -replace input value i.e emoticons (image) after post. How do I check user's input value to check to see if they have type these characters for example

":-)"
4

2 に答える 2

1

str_replace は一致するものすべてを置き換えるため、確認する必要はありません。投稿が空でないかどうかのみを確認してください。

<?php
if($_POST['textarea_name']!=""){
    $text = $_POST['textarea_name'];
    $chatText = str_replace(":D","<img src='happy.jpg' width='20' height='20' alt='Big Smile' />",$text);
    echo $chatText;
}
?>

配列を使用したより詳細な置換については、これを確認してください。

絵文字の置き換え - PHP

于 2013-04-26T14:28:32.310 に答える
1

次のようなものが正しく機能します。

foreach ($_POST as $k=>$v) {
    $_POST[$k] = str_replace(":D","<img src='happy.jpg' width='20' height='20' alt='Big Smile' />",$v);
}

それは単に送信された$_POST値を処理し、チャットボックスを使用しているため、そのようなメッセージボックスのみを処理する必要があるため、これらのテキストボックス内の:D文字を置き換えます。

于 2013-04-26T14:30:43.707 に答える