Question

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

":-)"
Was it helpful?

Solution

No need to check as str_replace will replace everything it matches, only check if the post isn't empty.

<?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;
}
?>

Check this out for more detailed replacement using arrays;

Emoticon Replacement - PHP

OTHER TIPS

Something like the following would work correctly for you,

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

That would simply process and $_POST values submitted, in which what i'm gathering is you're using a chat box, so it should only process such message box, Thus meaning it will replace and :D characters within these text boxes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top