Replace Character Using PHP With Source Code

Good day everyone! This time, I am just going to teach you some basic tutorial for replacing characters. But before that, I have a simple question to you. Do you ever try on posting a statement or message with emoticons/smileys? Usually, we do it when we are chatting with our friends on FACEBOOK. You just type the characters, for example, “ : ) ” without spaces then after that it will show the emoticon. Do you ever wonder how does it work? Well, this simple tutorial gives you some basic knowledge in regards to creating your own emoticon code and replace it to an emoticon.

Before that, I will give you first some knowledge about a PHP function that we will use in creating our smiley code.

str_replace is PHP function which uses for replacing a character, word or statement into a new value.

For example:

We will replace small letter “b” into capital letter “B” or we will replace small letter “a” into an integer “1”.

*** REPLACING SMALL LETTER “b” into capital letter “B” ***

[php]

<?php

$letter = “b”;

$new_letter_format = “B”;

$statement = “boy”;

$convert = str_replace($letter, $new_letter_format, $statement);

echo $convert;

?>[/php]

Output:

Did you get the idea in replacing characters in PHP?

Now we will use that idea for developing our own smiley code.

So first, download emoticons, or create your own emoticon if you are good in graphic designing. Here, I just downloaded 5 emoticons which are angel, angry, cool, cry and smile.

So what we do now is to display any of 5 smileys you want to show without typing “<img src=”” />”. What we need is just to type its corresponding codes like “ : ) ” without spaces or maybe we “[smile]” or anything you want.

To do this, just write the following codes.

[php]

<?php

$emoticon_codes = array(“[angel]”, “[angry]”, “[cool]” “[cry]”, “[smile]”);

$emoticons  = array(“<img src=”angel.gif” />”, “<img src=”angry.gif” />”, “<img src=”cool.gif” />”, “<img src=”cry.gif” />”, “<img src=”[smile]” />”);

$statement = “Hi! [angel]”;

$convert_emoticon_code = str_replace($emoticon_codes, $emoticons, $statement);

echo $convert_emoticon_code;

?>[/php]

Output:

If you have questions about Replace Character Using PHP for Creating Personalized Smiley Codes 2016, feel free to comment here or visit our contact page. Thank you.

Leave a Comment