Follow us on Twitter!
The measure of a mans life is not how well he dies, but how well he lives.
Sunday, April 20, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 25
Guests Online: 25
Members Online: 0

Registered Members: 82850
Newest Member: hardstylurr
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Even more help with PHP

t0xikc0mputer
Member



Posts: 112
Location: t0xik waste dump
Joined: 07.01.11
Rank:
Newbie
Posted on 04-05-11 01:52
Hello again everyone,

It seems I keep requiring help with problems I cannot solve on my own with PHP, but that is the sign that I am learning right? Anyways, this only requires a simple answer as it is just another question on functions.

I am in the process of creating a program (to keep my programming skills up) that parses through user entered text and looks for words from an array that would be used in texting. (for example omg, lol, etc.)

Code
$dic = array("afaik" => "as far as i know",
"afk" => "away from keyboard",
"aka" => "also known as",
"asap" => "as soon as possible",
"atb" => "all the best",
"atk" => "at the keyboard",
"atm" => "at the moment",
"b4" => "before",
"bf" => "boy friend",
"brb" => "be right back",
"brt" => "be right there",
"btw" => "by the way",
"cu" => "see you",
"cul8r" => "see you later",
"cya" => "see you",
"dk" => "don't know",
"faq" => "frequently asked questions",
"fyi" => "for your information",
"gf" => "girlfirend",
"gg" => "good game",
"gr8" => "great",
"h8" => "hate",
"ic" => "i see",
"idk" => "i dont know",
"l8" => "late",
"l8r" => "later",
"lmao" => "laugh my ass off",
"lol" => "laughing out loud",
"m8" => "mate",
"myob" => "mind your own business",
"ne" => "any",
"ne1" => "anyone",
"pita" => "pain the the ass",
"ppl" => "people",
"r" => "are",
"rofl" => "rolling on the floor laughing",
"roflol" => "rolling on the floor laughing out loud",
"rotflmao" => "rolling on the floor laughing my ass off",
"ru" => "are you",
"sk8" => "skate",
"thx" => "thank you",
"ttyl" => "talk to you later",
"u" => "you",
"u2" => "you too",
"ur" => "you are",
"w8" => "wait",
"wtf" => "what the f***",
"wtg" => "way to go!");
strtolower($text);
$text = explode(" ", $text);
foreach ($text as $key => $var) {
$var1 = $var;
$key1 = $key;
foreach ($dic as $key => $var) {
if ($key == $var1) {
$var1 = $var;
}
}
echo $var1;
}




At this point in time, as you can see, it is not quite done yet. The issue I am having is that the "explode();" function removes the spaces when I use it. Is there a way (or another function) that will split it up at the spaces without removing the spaces?

Thanks very much and sorry to bother with simple questions,

~t0xik

On a side note: In the end I may submit this code to the code bank for variation by more experienced ones who may want to take it to another level. (Who knows)


Author

RE: Even more help with PHP

starofale
Member



Posts: 218
Location: England
Joined: 05.12.07
Rank:
Moderate
Posted on 04-05-11 02:32
If I understand correctly, you get a string from the user with text-style words in it and you convert these words to their expanded versions. e.g. user inputs "omg ic", you return "oh my god i see".
In that case, why do you need to keep the spaces? All the text phrases are single words in the user input - "omg" has no spaces in it.

I don't see how a function that splits at the spaces but doesn't remove them could be helpful, even if you wanted to convert the other way ("oh my god i see" -> "omg ic"). It would still have each word as an individual element in the list/array (or whatever it's called in php), just with a space at the beginning or end depending on how the function was defined. e.g. "oh my god i c" -> ("oh ", "my ", "god ", "i ", "c")

Please correct me if anything I have said is incorrect.

Also:
t0xikc0mputer wrote:
"lmao" => "laughing my ass off"


t0xikc0mputer wrote:
(for example omg, lol, etc.)

You missed out omg from the list.


Finally, why does it say your post was written on 04-05-11 01:52, but last edited on 29-04-11 19:56? A bug with HBH forums?


Try a new search engine

Edited by starofale on 04-05-11 02:34
Author

RE: Even more help with PHP


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 04-05-11 04:36
starofale wrote:
In that case, why do you need to keep the spaces? All the text phrases are single words in the user input - "omg" has no spaces in it.

I don't see how a function that splits at the spaces but doesn't remove them could be helpful

My guess: he just didn't realise that he can actually add a space himself for output... not that that would help him anything since he's taking words from a list he defined himself.

It would still have each word as an individual element in the list/array (or whatever it's called in php)

It's a map, though called an array within PHP.

Also, that code at the end is awful.
You could do something like this instead.
Code
foreach ($text as $key => $var)
   echo (array_key_exists($var, $dic)?$dic[$var]:$var).' ';



Or if you're pants-shittingly afraid of trailing spaces:
Code
$outp;
foreach ($text as $key => $var)
   $outp.=(array_key_exists($var, $dic)?$dic[$var]:$var).' ';
echo trim($outp);





Author

RE: Even more help with PHP

stealth-
Member



Posts: 1003
Location: Eh?
Joined: 10.04.09
Rank:
Mad User
Posted on 04-05-11 05:14
COM wrote:
Code
$outp;
foreach ($text as $key => $var)
   $outp.=(array_key_exists($var, $dic)?$dic[$var]:$var).' ';
echo trim($outp);




Is that "$outp;" some sort of variable declaration? I didn't think it was necessary to do that with PHP, afaik you can simply append to a variable and it will be created, if necessary.


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealt. . .
http://www.stealth-x.com
Author

RE: Even more help with PHP


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 04-05-11 05:32
stealth- wrote:
Is that "$outp;" some sort of variable declaration? I didn't think it was necessary to do that with PHP, afaik you can simply append to a variable and it will be created, if necessary.

Yeah it is, thanks for that, didn't know. Keep in mind, I'm sort of shit with PHP, didn't think a variable declared inside a loop would exist outside of it.


Author

RE: Even more help with PHP

stealth-
Member



Posts: 1003
Location: Eh?
Joined: 10.04.09
Rank:
Mad User
Posted on 04-05-11 05:41
COM wrote:
Yeah it is, thanks for that, didn't know. Keep in mind, I'm sort of shit with PHP, didn't think a variable declared inside a loop would exist outside of it.


Ah, okay. No worries, I was just curious about it, as I wasn't even aware just a variable like that was valid code.


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealt. . .
http://www.stealth-x.com
Author

RE: Even more help with PHP

ADIGA
Member



Posts: 57
Location: Jordan - Middle East
Joined: 28.12.07
Rank:
Mad User
Posted on 04-05-11 06:56
At this point in time, as you can see, it is not quite done yet. The issue I am having is that the "explode();" function removes the spaces when I use it. Is there a way (or another function) that will split it up at the spaces without removing the spaces?


explode will have the text in an array, search it for what you want to replace and do the replacement on the array level, like if brb is element # 15 in the text array, make the array element #15 "be right back" and after done do implode(" ", $text);
that way you keep the space.


www.hackthis.co.uk/users/images/210/1:1/50/56ba5115dde42ff71f426149a02fc0b8.jpg
adiga_php@hotmail.com adiga.hacker@yahoo.com www.adiga.ws
Author

RE: Even more help with PHP

t0xikc0mputer
Member



Posts: 112
Location: t0xik waste dump
Joined: 07.01.11
Rank:
Newbie
Posted on 04-05-11 12:40
starofale wrote:
t0xikc0mputer wrote:
"lmao" => "laughing my ass off"

Oh, didn't even notice that. Thanks!

starofale wrote:
t0xikc0mputer wrote:
(for example omg, lol, etc.)

You missed out omg from the list.

Once again, thank you for your astute observations.

starofale wrote
why does it say your post was written on 04-05-11 01:52, but last edited on 29-04-11 19:56? A bug with HBH forums?

That is part of my sig. I made it partly to confuse people, but it says the last time I edited my sig. (In this case, when I got the new avatar I wrote "Check out the new logo!")

COM wrote:
starofale wrote:
In that case, why do you need to keep the spaces? All the text phrases are single words in the user input - "omg" has no spaces in it.

I don't see how a function that splits at the spaces but doesn't remove them could be helpful

My guess: he just didn't realise that he can actually add a space himself for output... not that that would help him anything since he's taking words from a list he defined himself.

Yes, and yes. But I didn't want to go around adding spaces to everything on the list for a program that might not go anywhere, so I didn't.

COM wrote:
Also, that code at the end is awful.
You could do something like this instead.
Code
foreach ($text as $key => $var)
   echo (array_key_exists($var, $dic)?$dic[$var]:$var).' ';



Or if you're pants-shittingly afraid of trailing spaces:
Code
$outp;
foreach ($text as $key => $var)
   $outp.=(array_key_exists($var, $dic)?$dic[$var]:$var).' ';
echo trim($outp);



Ah, thank you. Your code worked 'pants-shittingly' beautifully. But there is one small part that I don't understand that (for the sake of learning) I would really appreciate if you (or someone else) explained.
That would be this part:

Code
?$dic[$var]:$var




ADIGA wrote:
At this point in time, as you can see, it is not quite done yet. The issue I am having is that the "explode();" function removes the spaces when I use it. Is there a way (or another function) that will split it up at the spaces without removing the spaces?


explode will have the text in an array, search it for what you want to replace and do the replacement on the array level, like if brb is element # 15 in the text array, make the array element #15 "be right back" and after done do implode(" ", $text);
that way you keep the space.

I would do that, but I have already changed my idea of the purpose and use of the program. I will keep your idea in mind, but COM's seemed shorter for this particular program.

COM wrote:
I'm sort of shit with PHP

If you're shit with PHP, then what does that make me?!

Thank you to everyone who submitted helpful responses. I really appreciate it.

~t0xik


Author

RE: Even more help with PHP


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 04-05-11 14:13
t0xikc0mputer wrote:
there is one small part that I don't understand that (for the sake of learning) I would really appreciate if you (or someone else) explained.
That would be this part:

Code
?$dic[$var]:$var




It's called a ternary operator and essentially works likte this:
<boolean statement> ? <value to use if true> : <value to use if false>

I would do that, but I have already changed my idea of the purpose and use of the program. I will keep your idea in mind, but COM's seemed shorter for this particular program.

ADIGA's version isn't too far from the code I gave you, except that instead of adding to a string, you'd change the value in the text array (in the event that the key exists) to what it is in full length, then do an echo implode(' ', $text) instead of an echo trim($outp).


Author

RE: Even more help with PHP

t0xikc0mputer
Member



Posts: 112
Location: t0xik waste dump
Joined: 07.01.11
Rank:
Newbie
Posted on 04-05-11 21:14
COM wrote:
t0xikc0mputer wrote:
there is one small part that I don't understand that (for the sake of learning) I would really appreciate if you (or someone else) explained.
That would be this part:

Code
?$dic[$var]:$var




It's called a ternary operator and essentially works likte this:
<boolean statement> ? <value to use if true> : <value to use if false>

Ah, thank you so much for clarifying, I will use this forever after.

EDIT: I have revised the dictionary, and it is all working well! Thanks for your help to everyone who suggested things.

Also, in the future, I will just post php questions here instead of making other threads for the sake of simplicity.




Edited by t0xikc0mputer on 04-05-11 22:18
Author

RE: Even more help with PHP


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 04-05-11 22:21
t0xikc0mputer wrote:
Ah, thank you so much for clarifying, I will use this forever after.

You're welcome, also, there's one more thing about it that's fun that I forgot to mention and that is that ternary operators can be nested. It's quick, neat, useful and fantastic at confusing the shit out of people.


Author

RE: Even more help with PHP

t0xikc0mputer
Member



Posts: 112
Location: t0xik waste dump
Joined: 07.01.11
Rank:
Newbie
Posted on 05-05-11 00:46
I also think that I may make a variation of the text translator project with hacker terms so that the n00bs can understand better. (no seriously in-depth ones, just like n00b, 1337, etc.)


Author

RE: Even more help with PHP

ADIGA
Member



Posts: 57
Location: Jordan - Middle East
Joined: 28.12.07
Rank:
Mad User
Posted on 05-05-11 11:02
t0xikc0mputer wrote:
I also think that I may make a variation of the text translator project with hacker terms so that the n00bs can understand better. (no seriously in-depth ones, just like n00b, 1337, etc.)


It would be a better way of learning, but i dont think that is any good for anything but learning, those who use stuff like "n00b, 1337" are idiots, at least thats how i see them, impress me with a script or a program, not geek talk!


www.hackthis.co.uk/users/images/210/1:1/50/56ba5115dde42ff71f426149a02fc0b8.jpg
adiga_php@hotmail.com adiga.hacker@yahoo.com www.adiga.ws
Author

RE: Even more help with PHP


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 06-05-11 12:19
I have a php question so I decided to post it here.

Code

for ($i=1;$i<=3;$i++)
  {
  $y = $previousvariable['Y']-$i;
  $location = mysql_fetch_array(mysql_query("SELECT ID FROM TheTable WHERE LocationX='{$previousvariable['X']}' AND Y='{$y}' AND Z='{$previousvariable['Z']}'"));

  if ($location['ID'] != 'solid')
    {
    mysql_query("UPDATE TheTable SET Y='{$y}' WHERE player='{$previousvariable['ID']}'");
    }
  }





Rather than making so many calls to the database and updating the co ordinates everytime it checks to see if the area is being occupied, How can I check all 3 variations at once.

Let me give you an example so you can better understand. Lets say I want to move 3 spaces. Each space could possibly have a solid space (I can't move on a solid space). So I want to move over 1 space. If space 2 is free, I want to move to space two. If space 3 is free, I want to move to space 3. Kinda get it? If space one, two, and three are free, then just move the 3 spaces.
Author

RE: Even more help with PHP

stealth-
Member



Posts: 1003
Location: Eh?
Joined: 10.04.09
Rank:
Mad User
Posted on 06-05-11 16:21
The SQL "OR" statement might be useful to you here, Froger. You can return all three values of Y in the same query, check to see which ones are valid movement points, and send your update query with the highest valid Y value you found. Something like below, maybe?

Code
$newY = 0;
$query = "SELECT ID, Y FROM TheTable WHERE";
$query .= ' LocationX = ' . $previousvariable['X'];
$query .= ' AND Z = ' . $previousvariable['Z'];
$query .= ' AND Y = ' $previousvariable['Y']-1;
$query .= ' OR Y = ' $previousvariable['Y']-2;
$query .= ' OR Y = ' $previousvariable['Y']-3;

$r = mysql_query($query, $connection);

while (($location = mysql_fetch_array($r)))
{
   if ($location['ID'] != 'solid' &&
       $location['Y'] > $newY)
      $newY = $location['Y'];
}

if ($newY) mysql_query('UPDATE TheTable SET Y = ' . $newY . ' WHERE player = ' . $previousvariable['ID']);





The $newY variable is only updated if the row being returned has a higher $location['Y'] value than is already in there (and is not solid, of course), therefore you will end up with the greatest possible movement in $newY at the end of the while loop. The update query is also only sent if a valid Y value is found.

It's a little early here, and I'm still waking up, so you'll probably want to check that code first as I just sorta wrote it on the spot. Anyway, hope that helps ;)

EDIT: I just realized that if you are subtracting from the players current position, then you may want something like "$location['Y'] < $newY" instead, since you are wanting the lowest value. Anyway, I'm sure you get the idea.


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealt. . .

Edited by stealth- on 06-05-11 16:36
http://www.stealth-x.com
Author

RE: Even more help with PHP


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 06-05-11 16:42
stealth- wrote:
Code

$newY = 0;

$r = mysql_query($query, $connection);

while (($location = mysql_fetch_array($r)))
{
   if ($location['ID'] != 'solid' &&
       $location['Y'] > $newY)
      $newY = $location['Y'];
}

if ($newY) mysql_query('UPDATE TheTable SET Y = ' . $newY . ' WHERE player = ' . $previousvariable['ID']);






Thanks stealth for the response. This is actually exactly what I was looking for. Much appreciated. I start a project and end up spending several hours on it, then BAM I go brain dead. Lol its like my computer is like give me a fucking break dude.
Author

RE: Even more help with PHP

stealth-
Member



Posts: 1003
Location: Eh?
Joined: 10.04.09
Rank:
Mad User
Posted on 07-05-11 00:16
Yup, glad to help Smile And yeah, I think every coder has gotten way more consumed with a project than they meant to at some point or time in their life, lol.


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealt. . .
http://www.stealth-x.com
Author

RE: Even more help with PHP

goluhaque
Member



Posts: 197
Location: India
Joined: 17.02.10
Rank:
Apprentice
Warn Level: 30
Posted on 08-05-11 14:40
stealth- wrote:
Yup, glad to help Smile And yeah, I think every coder has gotten way more consumed with a project than they meant to at some point or time in their life, lol.

Agreed. Like a stupid CMS.


That applause I receive from y'all on posting this post would have gotten me drunk on power if I hadn't already been high on life.