![]() |
Some PHP help, please?
So I have this array like this
Code:
[location] => 2020-10-16 04:08PM -0700 Code:
$city = 'Las Vegas'; |
Quote:
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 |
Quote:
Thanks for the help. What is the +4, +6, and +8 for? |
$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]); |
Quote:
|
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 ) |
Quote:
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. . |
Thanks guys, I've got enough here to get it done now :thumbsup
|
Quote:
|
I wouldn't try anything before coffee.
|
There's a java joke in here somewhere.. :food-smil10
|
Quote:
|
Just wait till i'm in charge around here. You are all going to regret making fun of me.
Dammit. . |
Quote:
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..... |
Quote:
I debugged it and found the issues before K0nr4d said anything dammit. Fuckers picking on me. . |
Ballers hire people to do that
|
Quote:
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... |
Quote:
|
Quote:
Sorry Mr Pheer - Totally derailed your thread..... Spunky - You apolligise as well... |
this reminds my why i switched to nodejs from php, makes this like this much simpler
|
Quote:
|
Quote:
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" |
Quote:
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? . |
No one ever said PHP was a good programming language...
https://media1.giphy.com/media/94EQmVHkveNck/source.gif |
Quote:
|
Looks like json, maybe you should ask for all the data.
|
Quote:
|
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!
|
Quote:
I did COBOL for a little bit, too. Wasn't paying enough. |
Quote:
|
Quote:
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. . |
Quote:
|
Quote:
Code:
preg_match('/City: (.*?)\n.*?Region: (.*?)\n.*?Zip Code: (.*?)$/s', $location, $matches); 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. |
Quote:
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 :1orglaugh |
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? ;) |
Quote:
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. |
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 :)
|
It's good to see k0nr4d helping people :)
|
All times are GMT -7. The time now is 02:10 PM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc