Author Topic: Explain this piece of PHP code to me  (Read 602 times)

0 Members and 1 Guest are viewing this topic.

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Explain this piece of PHP code to me
« on: July 31, 2014, 06:11:39 pm »
I wrote this, but I can't understand how it works. This is the code:
Code: (php) [Select]
public function extractHostname($rawOutput) {
    $start = strpos($rawOutput, '@')+1;
    print $start."\n";
    $end = strpos($rawOutput, ' ');
    print $end."\n";
    print $end-strlen($rawOutput)."\n";
    return strtolower(substr($rawOutput, $start, $end-strlen($rawOutput)));
}
Sample input:
Code: [Select]
:Kulverstukas!Geek@whore.house PRIVMSG #test :!yes
So, $start is 19, $end is 30, $end-strlen() is -22. substr gets everything from position 19 to -22 and it gets it correctly.

What the hell is going on?
« Last Edit: July 31, 2014, 06:11:58 pm by Kulverstukas »

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: Explain this piece of PHP code to me
« Reply #1 on: July 31, 2014, 06:28:02 pm »
the reason you get that reult is because your starting parameter is not 0, but 19. Soooo, when you use return and all code there, you acctualy said, return string starting from position 19 to position -22. But if you want to go from beginning, just type 0 instead of $start.
Vae Victis - suffering to the conquered

Offline Fur

  • Knight
  • **
  • Posts: 216
  • Cookies: 34
    • View Profile
Re: Explain this piece of PHP code to me
« Reply #2 on: July 31, 2014, 06:30:08 pm »
From the PHP docs:
Quote
If length is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative). If start denotes the position of this truncation or beyond, false will be returned.
So from what I understand, if $length is negative then $length characters will be removed from the end of the string.
However, the last character of 'whore.house' is 20 characters away from the end. Maybe a line break or something is also being passed?

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: Explain this piece of PHP code to me
« Reply #3 on: July 31, 2014, 06:42:50 pm »
But if you ask how your code works, here is how. You created function with one parameter. Now that parameter is string. okay, so you created variable start, that will take value of position where @ is. If position of @ is 18 like yours, you added plus one and get 19. Now, you simply printed out that value on the screen. next thing you did is you created variable end which will take value of position where space first happen. and in your case it's position 30. So you again print that out. Next thing you did is substruction btw variable end and length of full string. And output is -22 in your case. Now, you returned all lower case string, starting from position 19 to -22. and that's prerty much it. Is that what you asked for?

Are you sure that result of variable end minus length of string is -22? As far as I counted it's -20.
« Last Edit: July 31, 2014, 07:50:47 pm by Kulverstukas »
Vae Victis - suffering to the conquered

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Explain this piece of PHP code to me
« Reply #4 on: July 31, 2014, 07:55:40 pm »
Well, I forgot to mention that a line break (\r\n) might also be passed with the string.
@Code.Illusionist: I was asking about the logic, I understand what the functions do.

But I understand now, thanks Fur. How it works is that it truncates the string -22 chars, so up to ":Kulverstukas!Geek@whore.house" and substr() returns everything from 19th char(@) + 1, that's how it gets the correct substring. I totally skipped that part about truncation if a negative index is given, lol.