Some PHP help, please?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mr Pheer
    So Fucking Banned
    • Dec 2002
    • 22083

    #1

    Some PHP help, please?

    So I have this array like this

    Code:
    [location] => 2020-10-16 04:08PM -0700
    City: Las Vegas
    Region: Nevada
    Zip Code: 89149
    What I want to do, is make it into three separate variables, like this

    Code:
    $city = 'Las Vegas';
    $region = 'Nevada';
    $zipcode = '89149';
    How do I do that?
  • sarettah
    see you later, I'm gone
    • Oct 2002
    • 14297

    #2
    Originally posted by Mr Pheer
    So I have this array like this

    Code:
    [location] => 2020-10-16 04:08PM -0700
    City: Las Vegas
    Region: Nevada
    Zip Code: 89149
    What I want to do, is make it into three separate variables, like this

    Code:
    $city = 'Las Vegas';
    $region = 'Nevada';
    $zipcode = '89149';
    How do I do that?

    Hey, I just woke up so can't write the code but I just go through and parse it out based on the words.

    something along the lines of:

    $location='2020-10-16 04:08PM -0700
    City: Las Vegas
    Region: Nevada
    Zip Code: 89149';

    $location=strtolower($location);
    $city=substr($location,strpos($location,'city')+4) ;
    $city=substr($city,0,strpos($city,'region');

    $region=substr($location,strpos($location,'region' )+6);
    $region=substr($region,0,strpos($region,'region');

    $zip=substr($location,strpos($location,'zip code')+8);


    There is probably a more elegant way but like I said, I just woke up ;p
    All cookies cleared!

    Comment

    • Mr Pheer
      So Fucking Banned
      • Dec 2002
      • 22083

      #3
      Originally posted by sarettah
      Hey, I just woke up so can't write the code but I just go through and parse it out based on the words.
      I'm only up because I was coding shit and didn't sleep last night

      Thanks for the help. What is the +4, +6, and +8 for?

      Comment

      • k0nr4d
        Confirmed User
        • Aug 2006
        • 9231

        #4
        $location="2020-10-16 04:08PM -0700
        City: Las Vegas
        Region: Nevada
        Zip Code: 89149";

        $replace= array("City: ","Region: ","Zip Code: ");
        $location = str_replace($replace,"",$location);
        $arr = explode("\n",$location);
        $city = trim($arr[1]);
        $region = trim($arr[2]);
        $zipcode = trim($arr[3]);
        Mechanical Bunny Media
        Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

        Comment

        • k0nr4d
          Confirmed User
          • Aug 2006
          • 9231

          #5
          Originally posted by Mr Pheer
          I'm only up because I was coding shit and didn't sleep last night

          Thanks for the help. What is the +4, +6, and +8 for?
          It's to offset the number of characters but it's wrong, because he didn't factor in the : and the space after it. He also left out two )'s
          Mechanical Bunny Media
          Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

          Comment

          • k0nr4d
            Confirmed User
            • Aug 2006
            • 9231

            #6
            Another way if the order isn't identical each time.

            <?php
            $location="2020-10-16 04:08PM -0700
            City: Las Vegas
            Region: Nevada
            Zip Code: 89149";

            $location = explode("\n",$location);
            unset($location[0]);
            foreach($location as $i) {
            $out = explode(":",$i);
            $dataOut[trim($out[0])] = trim($out[1]);
            }
            print_r($dataOut);

            Array ( [City] => Las Vegas [Region] => Nevada [Zip Code] => 89149 )
            Mechanical Bunny Media
            Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

            Comment

            • sarettah
              see you later, I'm gone
              • Oct 2002
              • 14297

              #7
              Originally posted by k0nr4d
              It's to offset the number of characters but it's wrong, because he didn't factor in the : and the space after it. He also left out two )'s
              You do it before coffee damnit.

              However if you are doing an array it is probably more like this:

              $location=array('2020-10-16 04:08PM -0700
              City: Las Vegas
              Region: Nevada
              Zip Code: 89149',
              '2020-10-16 04:08PM -0700
              City: Las Vegas2
              Region: Nevada2
              Zip Code: 89150',
              '2020-10-16 04:08PM -0700
              City: Las Vegas3
              Region: Nevada3
              Zip Code: 89151',
              '2020-10-16 04:08PM -0700
              City: Las Vegas4
              Region: Nevada4
              Zip Code: 89152');

              $newlocation=array();
              foreach($location as $loc)
              {
              $city=substr($loc,strpos($loc,'city')+5);
              $newlocation[]['city']=substr($city,0,strpos($city,'region'));

              $region=substr($loc,strpos($loc,'region')+7);
              $newlocation[]['region']=substr($region,0,strpos($region,'region'));

              $newlocation[]['zip code']=substr($loc,strpos($loc,'zip code')+9);
              }

              var_dump($newlocation);


              -------------------------

              And that one doesn't leave off the )'s and it assumes that the order of the vars is the same.

              .
              All cookies cleared!

              Comment

              • Mr Pheer
                So Fucking Banned
                • Dec 2002
                • 22083

                #8
                Thanks guys, I've got enough here to get it done now

                Comment

                • CaptainHowdy
                  Too lazy to set a custom title
                  • Dec 2004
                  • 94732

                  #9
                  Originally posted by sarettah
                  You do it before coffee damnit.

                  Comment

                  • ruff
                    I have a plan B
                    • Aug 2004
                    • 5507

                    #10
                    I wouldn't try anything before coffee.
                    CryptoFeeds

                    Comment

                    • Colmike9
                      (>^_^)b
                      • Dec 2011
                      • 7230

                      #11
                      There's a java joke in here somewhere..
                      Join the BEST cam affiliate program on the internet!
                      I've referred over $1.7mil in spending this past year, you should join in.
                      I make a lot more money in the medical field in a lab now, fuck you guys. Don't ask me to come back, but do join Chaturbate in my sig, it still makes bank without me touching shit for years..

                      Comment

                      • EddyTheDog
                        Just Doing My Own Thing
                        • Jan 2011
                        • 25433

                        #12
                        Originally posted by Colmike7
                        There's a java joke in here somewhere..
                        Don't Java before Java? - I know it's PHP but it's the best I could do - I haven't had any coffee yet...

                        Comment

                        • sarettah
                          see you later, I'm gone
                          • Oct 2002
                          • 14297

                          #13
                          Just wait till i'm in charge around here. You are all going to regret making fun of me.


                          Dammit.

                          .
                          All cookies cleared!

                          Comment

                          • EddyTheDog
                            Just Doing My Own Thing
                            • Jan 2011
                            • 25433

                            #14
                            Originally posted by sarettah
                            Just wait till i'm in charge around here. You are all going to regret making fun of me.


                            Dammit.

                            .
                            Sorry - ...

                            I love you even though you can't count to 6.....

                            If you get stuck again it's the number of fingers you have on your left hand...









                            .

                            Sorry again, I'm in one of those moods.....

                            Comment

                            • sarettah
                              see you later, I'm gone
                              • Oct 2002
                              • 14297

                              #15
                              Originally posted by EddyTheDog
                              Sorry - ...

                              I love you even though you can't count to 6.....

                              If you get stuck again it's the number of fingers you have on your left hand...

                              Sorry again, I'm in one of those moods.....
                              They did not require any math for a programming degree (believe it or not)

                              I debugged it and found the issues before K0nr4d said anything dammit.

                              Fuckers picking on me.

                              .
                              All cookies cleared!

                              Comment

                              • Spunky
                                I need a beer
                                • Jun 2002
                                • 133986

                                #16
                                Ballers hire people to do that

                                Comment

                                • EddyTheDog
                                  Just Doing My Own Thing
                                  • Jan 2011
                                  • 25433

                                  #17
                                  Originally posted by Spunky
                                  Ballers hire people to do that
                                  I disagree - Real BALLERS come to GFY and ask - That's why they are BALLERS - You could pay a grand for that info, a grand you can invest - Maybe if you are millions in you can waste money like that...

                                  Lets see - I could pay a grand for some company to come back to me a week later - Or post on GFY and get the answer within a few hours - A few years ago it would have been minutes.....

                                  I was going to do a Trump joke - I would get told off though...

                                  Comment

                                  • Spunky
                                    I need a beer
                                    • Jun 2002
                                    • 133986

                                    #18
                                    Originally posted by EddyTheDog
                                    I disagree - Real BALLERS come to GFY and ask - That's why they are BALLERS - You could pay a grand for that info, a grand you can invest - Maybe if you are millions in you can waste money like that...

                                    Lets see - I could pay a grand for some company to come back to me a week later - Or post on GFY and get the answer within a few hours - A few years ago it would have been minutes.....

                                    I was going to do a Trump joke - I would get told off though...
                                    Mr Pheer wipes his ass with dolla bills

                                    Comment

                                    • EddyTheDog
                                      Just Doing My Own Thing
                                      • Jan 2011
                                      • 25433

                                      #19
                                      Originally posted by Spunky
                                      Mr Pheer wipes his ass with dolla bills
                                      I wipe my ass with your 2 cents...

                                      Sorry Mr Pheer - Totally derailed your thread.....

                                      Spunky - You apolligise as well...

                                      Comment

                                      • shake
                                        frc
                                        • Jul 2003
                                        • 4663

                                        #20
                                        this reminds my why i switched to nodejs from php, makes this like this much simpler
                                        Crazy fast VPS for $10 a month. Try with $20 free credit

                                        Comment

                                        • Mr Pheer
                                          So Fucking Banned
                                          • Dec 2002
                                          • 22083

                                          #21
                                          Originally posted by shake
                                          this reminds my why i switched to nodejs from php, makes this like this much simpler
                                          Yeah and I switched to Python as was blown away by it's simplicity. But sometimes shit just has to be done in PHP and I can't remember much of it because I hardly ever write it anymore.

                                          Comment

                                          • Mr Pheer
                                            So Fucking Banned
                                            • Dec 2002
                                            • 22083

                                            #22
                                            Originally posted by sarettah
                                            They did not require any math for a programming degree (believe it or not)

                                            I debugged it and found the issues before K0nr4d said anything dammit.

                                            Fuckers picking on me.

                                            .
                                            Really? No math?

                                            My kid is in for his Bachelor's in Computer Science at UNLV and they have him in Calculus 2 and I'm like, wtf you need that for?

                                            They also don't teach him anything except for C programming. When I ask him for PHP help he tells me "that's web shit, dude"

                                            Comment

                                            • sarettah
                                              see you later, I'm gone
                                              • Oct 2002
                                              • 14297

                                              #23
                                              Originally posted by Mr Pheer
                                              Really? No math?

                                              My kid is in for his Bachelor's in Computer Science at UNLV and they have him in Calculus 2 and I'm like, wtf you need that for?

                                              They also don't teach him anything except for C programming. When I ask him for PHP help he tells me "that's web shit, dude"
                                              Yeah, I did not do computer science. I probably should have.

                                              Lol at the C attitude. A friend of mine has a kid who is self taught C but unemployed. I told him I could throw him some PHP work and he was like "I only do C". My reply was "How much you makking doing that?". He didn't like that.

                                              Not putting down C programmers at all there. I program in C when I have to. I also do some python, hell I still do foxpro, cobol, VB, whatever language is needed.

                                              However, if you want to work for yourself, the easiest work to find is PHP because so much of the web stuff is written in PHP, that simple, ya know?

                                              .
                                              All cookies cleared!

                                              Comment

                                              • k0nr4d
                                                Confirmed User
                                                • Aug 2006
                                                • 9231

                                                #24
                                                No one ever said PHP was a good programming language...

                                                Mechanical Bunny Media
                                                Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

                                                Comment

                                                • EddyTheDog
                                                  Just Doing My Own Thing
                                                  • Jan 2011
                                                  • 25433

                                                  #25
                                                  Originally posted by k0nr4d
                                                  No one ever said PHP was a good programming language...

                                                  From you that's fucking hilarious!..

                                                  Comment

                                                  • grumpy
                                                    Too lazy to set a custom title
                                                    • Jan 2002
                                                    • 9870

                                                    #26
                                                    Looks like json, maybe you should ask for all the data.
                                                    Don't let greediness blur your vision | You gotta let some shit slide
                                                    icq - 441-456-888

                                                    Comment

                                                    • baddog
                                                      So Fucking Banned
                                                      • Apr 2001
                                                      • 107089

                                                      #27
                                                      Originally posted by Colmike7
                                                      There's a java joke in here somewhere..

                                                      Comment

                                                      • ruff
                                                        I have a plan B
                                                        • Aug 2004
                                                        • 5507

                                                        #28
                                                        When I was in school the only option was Fortran. So try some of that programming, you could work for NASA or Elon Musk. Of course, you're going to need more than one cup of coffee!
                                                        CryptoFeeds

                                                        Comment

                                                        • Colmike9
                                                          (>^_^)b
                                                          • Dec 2011
                                                          • 7230

                                                          #29
                                                          Originally posted by ruff
                                                          When I was in school the only option was Fortran. So try some of that programming, you could work for NASA or Elon Musk. Of course, you're going to need more than one cup of coffee!
                                                          Ugh, no thanks. In college I had to do Fortran 95 and Maple at the same time in one of the classes. It sucked.

                                                          I did COBOL for a little bit, too. Wasn't paying enough.
                                                          Join the BEST cam affiliate program on the internet!
                                                          I've referred over $1.7mil in spending this past year, you should join in.
                                                          I make a lot more money in the medical field in a lab now, fuck you guys. Don't ask me to come back, but do join Chaturbate in my sig, it still makes bank without me touching shit for years..

                                                          Comment

                                                          • fuzebox
                                                            making it rain
                                                            • Oct 2003
                                                            • 22351

                                                            #30
                                                            Originally posted by k0nr4d
                                                            No one ever said PHP was a good programming language...

                                                            Comment

                                                            • sarettah
                                                              see you later, I'm gone
                                                              • Oct 2002
                                                              • 14297

                                                              #31
                                                              Originally posted by Colmike7
                                                              Ugh, no thanks. In college I had to do Fortran 95 and Maple at the same time in one of the classes. It sucked.

                                                              I did COBOL for a little bit, too. Wasn't paying enough.
                                                              One semester I took Assembler, Advanced COBOL and BASIC. Assembler and COBOL were both 6 credit courses and BASIC was a normal 3 credit course. First day of class I went to the BASIC instructor and told him "I won't be in any classes but if you will tell me when the tests are I will show up for every one", and he said ok.

                                                              I spent all my free time in the computer lab that semester, lol. One test, he brought to me in the lab because I had somehow forgotten about it and missed class that night.

                                                              .
                                                              All cookies cleared!

                                                              Comment

                                                              • RyuLion
                                                                • Mar 2003
                                                                • 32369

                                                                #32
                                                                Originally posted by Colmike7
                                                                There's a java joke in here somewhere..
                                                                Programmer character at its best!

                                                                Adult Biz Consultant A tech head since 1995
                                                                Affiliate Support: Chaturbate | CCBill Live

                                                                Comment

                                                                • just a punk
                                                                  So fuckin' bored
                                                                  • Jun 2003
                                                                  • 32393

                                                                  #33
                                                                  Originally posted by Mr Pheer
                                                                  So I have this array like this

                                                                  Code:
                                                                  [location] => 2020-10-16 04:08PM -0700
                                                                  City: Las Vegas
                                                                  Region: Nevada
                                                                  Zip Code: 89149
                                                                  What I want to do, is make it into three separate variables, like this

                                                                  Code:
                                                                  $city = 'Las Vegas';
                                                                  $region = 'Nevada';
                                                                  $zipcode = '89149';
                                                                  How do I do that?
                                                                  Pff... Now look how the PHP pro's do it ;)

                                                                  Code:
                                                                  preg_match('/City: (.*?)\n.*?Region: (.*?)\n.*?Zip Code: (.*?)$/s', $location, $matches);
                                                                  array_shift($matches);
                                                                  list($city, $region, $zipcode) = $matches;
                                                                  Only 3 lines of code. We call it optimization, bro

                                                                  P.S. Some people say my code sux. That because it's mostly unreadable like the one above. They write for people, I write for machines. This is why it looks so strange, but it much more compact and faster than the code, most coders are used to produce (with all those classes etc). PHP is not C++ and the server resources are very limited.
                                                                  Obey the Cowgod

                                                                  Comment

                                                                  • k0nr4d
                                                                    Confirmed User
                                                                    • Aug 2006
                                                                    • 9231

                                                                    #34
                                                                    Originally posted by CyberSEO
                                                                    Only 3 lines of code. We call it optimization, bro
                                                                    While I will admit - short, elegant, you are firing up the regex engine. I actually timed the execution for shits to see which works fastest (adding an echo of the output at the end for fairness in each):

                                                                    0.0002028942108 (konrad v1)
                                                                    0.0002288818359 (konrad v2)
                                                                    0.0003898143768 (cyberseo)
                                                                    0.0011749267578 (sarettah)

                                                                    We are of course talking about 1/10000ths of a second difference here
                                                                    Mechanical Bunny Media
                                                                    Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

                                                                    Comment

                                                                    • just a punk
                                                                      So fuckin' bored
                                                                      • Jun 2003
                                                                      • 32393

                                                                      #35
                                                                      Actually not Depends on which operating system and which PHP engine you run it ;) And maybe in this particular case your code will be a bit faster (but the PHP code itself is larger). However in the more complicated cases (which we usually have a deal with, right?), regexp is much more effective by speed and that's a fact.

                                                                      So, if you want to do a string replace or find a fixed text, course you will be using simple string functions. But if you need to find some brain-breaking pattern, you will be using regexp. That's what it designed for.

                                                                      E.g. is the qsort algorithm the fastest and optimized one? You may say "not", because we can find lot of cases when it will be slow like a turtle, but... in general cases it faster that other more specific sorting algos. The same applies to regexp. Sometimes (in simplest cases) it really slower, but in general cases when you work with patterns, it's the fastest method. Don't you agree? ;)
                                                                      Obey the Cowgod

                                                                      Comment

                                                                      • k0nr4d
                                                                        Confirmed User
                                                                        • Aug 2006
                                                                        • 9231

                                                                        #36
                                                                        Originally posted by CyberSEO
                                                                        Actually not Depends on which operating system and which PHP engine you run it ;) And maybe in this particular case your code will be a bit faster (but the PHP code itself is larger). However in the more complicated cases (which we usually have a deal with, right?), regexp is much more effective by speed and that's a fact.

                                                                        If you want to do a string replace or find a fixed text, course you will be using simple string functions. But if you need to find some brain-breaking pattern, you will be using regexp. That's what it designed for.
                                                                        You are right - regex has it's place when it's a more complicated thing. In this particular case though string replace is faster. Either way like I said we're battling over 1/10000th of a second so in reality it makes no difference.

                                                                        It is interesting though how there's so many ways to do it and the huge variance in execution speed with one way taking 5-6x as long to execute despite basically being exactly the same thing.
                                                                        Mechanical Bunny Media
                                                                        Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

                                                                        Comment

                                                                        • Mr Pheer
                                                                          So Fucking Banned
                                                                          • Dec 2002
                                                                          • 22083

                                                                          #37
                                                                          Regex makes my head hurt. You fuck up one character and you've opened your script to shit like php injection hacks. Thanks for the code though

                                                                          Comment

                                                                          • PornDude
                                                                            I'm still broke.
                                                                            • Jul 2008
                                                                            • 3084

                                                                            #38
                                                                            It's good to see k0nr4d helping people
                                                                            PornDude.com 🔥

                                                                            PornWebmasters.com 🤑

                                                                            MyGaySites.com 🤭

                                                                            PornDudeCasting.com 🚀

                                                                            Comment

                                                                            Working...