Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

Even more help with PHP


t0xikc0mputer's Avatar
Member
0 0

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.)

"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)

starofale's Avatar
Member
0 0

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?


ghost's Avatar
0 0

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.

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

stealth-'s Avatar
Ninja Extreme
0 0

COM wrote:

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.

ghost's Avatar
0 0

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.


stealth-'s Avatar
Ninja Extreme
0 0

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.


ADIGA's Avatar
Member
0 0

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.


t0xikc0mputer's Avatar
Member
0 0

starofale wrote: [quote]t0xikc0mputer wrote: "lmao" => "laughing my ass off"[/quote] Oh, didn't even notice that. Thanks!

starofale wrote: [quote]t0xikc0mputer wrote: (for example omg, lol, etc.) You missed out omg from the list.[/quote] 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: [quote]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.[/quote] 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.

	echo (array_key_exists($var, $dic)?$dic[$var]:$var).' ';```
Or if you're pants-shittingly afraid of trailing spaces:
```markup$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:

```markup?$dic[$var]:$var```

> **ADIGA wrote:**
[quote]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.[/quote]
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

ghost's Avatar
0 0

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:

markup?$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).


t0xikc0mputer's Avatar
Member
0 0

COM wrote: [quote]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:

markup?$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>[/quote] 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.


ghost's Avatar
0 0

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.


t0xikc0mputer's Avatar
Member
0 0

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.)


ADIGA's Avatar
Member
0 0

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!


ghost's Avatar
0 0

I have a php question so I decided to post it here.

for ($i=1;$i&lt;=3;$i++)
  {
  $y = $previousvariable[&#39;Y&#39;]-$i;
  $location = mysql_fetch_array(mysql_query(&quot;SELECT ID FROM TheTable WHERE LocationX=&#39;{$previousvariable[&#39;X&#39;]}&#39; AND Y=&#39;{$y}&#39; AND Z=&#39;{$previousvariable[&#39;Z&#39;]}&#39;&quot;));

  if ($location[&#39;ID&#39;] != &#39;solid&#39;)
    {
    mysql_query(&quot;UPDATE TheTable SET Y=&#39;{$y}&#39; WHERE player=&#39;{$previousvariable[&#39;ID&#39;]}&#39;&quot;);
    }
  }

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.


stealth-'s Avatar
Ninja Extreme
0 0

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?

$query = &quot;SELECT ID, Y FROM TheTable WHERE&quot;;
$query .= &#39; LocationX = &#39; . $previousvariable[&#39;X&#39;];
$query .= &#39; AND Z = &#39; . $previousvariable[&#39;Z&#39;];
$query .= &#39; AND Y = &#39; $previousvariable[&#39;Y&#39;]-1;
$query .= &#39; OR Y = &#39; $previousvariable[&#39;Y&#39;]-2;
$query .= &#39; OR Y = &#39; $previousvariable[&#39;Y&#39;]-3;

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

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

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

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.


ghost's Avatar
0 0

stealth- wrote:

$newY = 0;

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

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

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

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.


stealth-'s Avatar
Ninja Extreme
0 0

Yup, glad to help :) 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.


goluhaque's Avatar
Member
0 0

stealth- wrote: Yup, glad to help :) 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.