Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use PHP in your chat box system to produce smiley?

Tags:

php

I am trying to analyse the text then when I saw ":)" I will replace it with the path of the image is there a way to make that happened?

Thanks in advance..

$text = "This would be fun :)"

wanted result = "This would be fun image path here"

so instead of ":)" I will see the path of the image to appear smiley

like image 337
Brandon Jake Sullano Avatar asked Feb 10 '23 01:02

Brandon Jake Sullano


2 Answers

$text = "This would be fun :)"
$newtext = str_replace(":)", "<img src='images/smiley.png'>", $text);

for multiple replace find example below from php.net

$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
// Provides Result: Hll Wrld f PHP
like image 115
kamlesh.bar Avatar answered Feb 12 '23 15:02

kamlesh.bar


Thanks kamlesh

as I read about str_replace I derived to this solution

  $str = "It would be fun :)";
     $image_path_smile = "<img class='emoticons' src='images/smile.png'/>";

    $pattern=array();
    $pattern[0]=":)";

    $replacement=array();
    $replacement[0]=$image_path_smile;

     str_replace($pattern,$replacement,$str);

Thanks for the quick response :)

like image 44
Brandon Jake Sullano Avatar answered Feb 12 '23 15:02

Brandon Jake Sullano