Replacing text with smilies! Getting some error!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

pHp_n0ob
Forum Commoner
Posts: 35
Joined: Mon Jul 09, 2012 7:30 am
Location: India

Replacing text with smilies! Getting some error!

Post by pHp_n0ob »

Hi,I'm a php n0ob want to be a great programmer,thats why doin practice :D

I'm tryin to replace text with smiles bt getting error,check this and plz guide me...

Code: Select all

<?php
$name=$_GET['name'];
$msz=$_GET['msz'];
$submit=$_GET['submit'];
$smilyChar=array(':p',':(',':D',':X');
$smilyName=array('smily/1.gif','smily/2.gif','smily/3.gif','smily/4.gif');
for($i=0;$i<=count($smilyChar);$i++)
$msz=str_replace($smilyChar[$i],'<img src="$smilyName[$i]" alt="$smilyChar[$i]">',$msz);
$msz=str_replace(':p','<img src="2.gif" alt=":p">',$msz);
echo $msz;?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Replacing text with smilies! Getting some error!

Post by Celauran »

Might be helpful if you actually mentioned what the error says...
pHp_n0ob
Forum Commoner
Posts: 35
Joined: Mon Jul 09, 2012 7:30 am
Location: India

Re: Replacing text with smilies! Getting some error!

Post by pHp_n0ob »

Nothing err0r...bt its n0t showing image....check this http://bdhitz.zerve.in/gb/main.php
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Replacing text with smilies! Getting some error!

Post by pickle »

variables aren't evaluated in single quotes.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
TildeHash
Forum Commoner
Posts: 43
Joined: Fri Jul 16, 2010 7:17 am
Location: Apple Valley, California

Re: Replacing text with smilies! Getting some error!

Post by TildeHash »

There were a few problems with it, but it should work as expected now:

Code: Select all

<form method="GET">
        <input type="text" name="name" value="Name" /><br />
        <input type="text" name="msz" value="Message" />
        <input type="submit" name="submit" value="Submit" /><br /><br />
<form>
<?php
$name = (isset($_GET['name'])) ? $_GET['name'] : 'Anonymous';
$msz = (isset($_GET['msz'])) ? $_GET['msz'] : 'Enter a Message';

$smilyChar = array(':p', ':(', ':D', ':X');
$smilyName = array('1.gif', '2.gif', '3.gif', '4.gif');

if (isset($_GET['msz'])) {
        for ($i = 0; $i != count($smilyChar); $i++) {
                $msz = str_replace($smilyChar[$i], '<img src="smily/' . $smilyName[$i] . '" alt="' . $smilyChar[$i] . '">', $msz);
        }
}
echo $msz . ' - ' . $name;
?>
EDIT: Oh "and guide you". First you shouldn't use "$_GET" nor "$_POST" etc, without checking to make sure they are set first. Variables can't be used within single quotes, use double quotes or break out of the single quotes like I do in the script. You didn't need to put the "smily/" in each of the array values, just in the final "$msz" variable. I noticed the unnecessary "$submit" variable, so I removed it. You also had an unnecessary second "$msz" variable, which I also removed. And you didn't make use of the "$name" variable, so I did. Other than that your method was excellent. :D
pHp_n0ob
Forum Commoner
Posts: 35
Joined: Mon Jul 09, 2012 7:30 am
Location: India

Re: Replacing text with smilies! Getting some error!

Post by pHp_n0ob »

There were a few problems with it, but it should work as expected now:
thanx...bro,i got everything except the one about for loop

why u used !== in for lo0p...can u plz explain...I know it means not equal to bt cant undrstnd in the code ...and thanks for the guidance also bro :D
User avatar
TildeHash
Forum Commoner
Posts: 43
Joined: Fri Jul 16, 2010 7:17 am
Location: Apple Valley, California

Re: Replacing text with smilies! Getting some error!

Post by TildeHash »

pHp_n0ob wrote:
There were a few problems with it, but it should work as expected now:
thanx...bro,i got everything except the one about for loop

why u used !== in for lo0p...can u plz explain...I know it means not equal to bt cant undrstnd in the code ...and thanks for the guidance also bro :D
There are three parts to a "for loop", the first is defining a variable, the second is stating a condition under which the loop runs, and the third is the action that is to be performed while the condition isn't met. So while "$i" is not equal to the number of array values in "$smilyChar", the loop increases the value of "$i" until it is equal to the number of array values in "$smilyChar". In this case there are four array values, so the loop will run until "$i" is equal to "4", which is four times. There isn't a need to set the condition to "if greater than" or "if lesser than" nor "if lesser than or equal to" because the "$i" variable will never go past the number of array values in "$smilyChar".

Hope I explained it clearly enough.
pHp_n0ob
Forum Commoner
Posts: 35
Joined: Mon Jul 09, 2012 7:30 am
Location: India

Re: Replacing text with smilies! Getting some error!

Post by pHp_n0ob »

Oh...thanx :p plz tell smthing ab0ut mysql...i'm gettin error...http://topnet.nstop.in/user_db.php
User avatar
TildeHash
Forum Commoner
Posts: 43
Joined: Fri Jul 16, 2010 7:17 am
Location: Apple Valley, California

Re: Replacing text with smilies! Getting some error!

Post by TildeHash »

pHp_n0ob wrote:Oh...thanx :p plz tell smthing ab0ut mysql...i'm gettin error...http://topnet.nstop.in/user_db.php
Sorry, I don't use databases (especially not MySQL.) 8)
pHp_n0ob
Forum Commoner
Posts: 35
Joined: Mon Jul 09, 2012 7:30 am
Location: India

Re: Replacing text with smilies! Getting some error!

Post by pHp_n0ob »

@samrem296 i didnt undrst0od what u said :D simply,i'm using free reseller web hosting cuz i'm learning n0w :D
pHp_n0ob
Forum Commoner
Posts: 35
Joined: Mon Jul 09, 2012 7:30 am
Location: India

Re: Replacing text with smilies! Getting some error!

Post by pHp_n0ob »

TildeHash wrote:There were a few problems with it, but it should work as expected now:

Code: Select all

<form method="GET">
        <input type="text" name="name" value="Name" /><br />
        <input type="text" name="msz" value="Message" />
        <input type="submit" name="submit" value="Submit" /><br /><br />
<form>
<?php
$name = (isset($_GET['name'])) ? $_GET['name'] : 'Anonymous';
$msz = (isset($_GET['msz'])) ? $_GET['msz'] : 'Enter a Message';

$smilyChar = array(':p', ':(', ':D', ':X');
$smilyName = array('1.gif', '2.gif', '3.gif', '4.gif');

if (isset($_GET['msz'])) {
        for ($i = 0; $i != count($smilyChar); $i++) {
                $msz = str_replace($smilyChar[$i], '<img src="smily/' . $smilyName[$i] . '" alt="' . $smilyChar[$i] . '">', $msz);
        }
}
echo $msz . ' - ' . $name;
?>
EDIT: Oh "and guide you". First you shouldn't use "$_GET" nor "$_POST" etc, without checking to make sure they are set first. Variables can't be used within single quotes, use double quotes or break out of the single quotes like I do in the script. You didn't need to put the "smily/" in each of the array values, just in the final "$msz" variable. I noticed the unnecessary "$submit" variable, so I removed it. You also had an unnecessary second "$msz" variable, which I also removed. And you didn't make use of the "$name" variable, so I did. Other than that your method was excellent. :D

plz also xplain ? and striplashes...thanx for ur help anyway :)
pHp_n0ob
Forum Commoner
Posts: 35
Joined: Mon Jul 09, 2012 7:30 am
Location: India

Re: Replacing text with smilies! Getting some error!

Post by pHp_n0ob »

TildeHash wrote:
pHp_n0ob wrote:Oh...thanx :p plz tell smthing ab0ut mysql...i'm gettin error...http://topnet.nstop.in/user_db.php
Sorry, I don't use databases (especially not MySQL.) 8)
8O
then how without databasess??? Do u use flat files instead :)
pHp_n0ob
Forum Commoner
Posts: 35
Joined: Mon Jul 09, 2012 7:30 am
Location: India

Re: Replacing text with smilies! Getting some error!

Post by pHp_n0ob »

TildeHash wrote:
pHp_n0ob wrote:Oh...thanx :p plz tell smthing ab0ut mysql...i'm gettin error...http://topnet.nstop.in/user_db.php
Sorry, I don't use databases (especially not MySQL.) 8)
8O
then how without databasess??? Do u use flat files instead
User avatar
TildeHash
Forum Commoner
Posts: 43
Joined: Fri Jul 16, 2010 7:17 am
Location: Apple Valley, California

Re: Replacing text with smilies! Getting some error!

Post by TildeHash »

pHp_n0ob wrote:
TildeHash wrote:
pHp_n0ob wrote:Oh...thanx :p plz tell smthing ab0ut mysql...i'm gettin error...http://topnet.nstop.in/user_db.php
Sorry, I don't use databases (especially not MySQL.) 8)
8O
then how without databasess??? Do u use flat files instead :)
If it's one's own content for one's own site, then any form of separated file storage isn't called for. I do use flat files, but I don't store other people's content or information very often so there is no need for me to use a database nor flat files very often, either. :)
pHp_n0ob
Forum Commoner
Posts: 35
Joined: Mon Jul 09, 2012 7:30 am
Location: India

Re: Replacing text with smilies! Getting some error!

Post by pHp_n0ob »

TildeHash" wrote:
TildeHash wrote: Sorry, I don't use databases (especially not MySQL.) 8)
8O
If it's one's own content for one's own site, then any form of separated file storage isn't called for. I do use flat files, but I don't store other people's content or information very often so there is no need for me to use a database nor flat files very often, either. :)
and what if you'l have to store data...say u have to make a forum...then what? Where you'l store the data?
Post Reply