GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Some PHP help, please? (https://gfy.com/showthread.php?t=1335808)

Mr Pheer 10-17-2020 05:39 AM

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? :eek7

sarettah 10-17-2020 06:04 AM

Quote:

Originally Posted by Mr Pheer (Post 22754733)
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? :eek7


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

Mr Pheer 10-17-2020 06:08 AM

Quote:

Originally Posted by sarettah (Post 22754738)
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?

k0nr4d 10-17-2020 06:09 AM

$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]);

k0nr4d 10-17-2020 06:12 AM

Quote:

Originally Posted by Mr Pheer (Post 22754742)
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

k0nr4d 10-17-2020 06:18 AM

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 )

sarettah 10-17-2020 06:20 AM

Quote:

Originally Posted by k0nr4d (Post 22754746)
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.

.

Mr Pheer 10-17-2020 06:32 AM

Thanks guys, I've got enough here to get it done now :thumbsup

CaptainHowdy 10-17-2020 05:47 PM

Quote:

Originally Posted by sarettah (Post 22754752)
You do it before coffee damnit.


ruff 10-17-2020 06:56 PM

I wouldn't try anything before coffee.

Colmike9 10-17-2020 07:12 PM

There's a java joke in here somewhere.. :food-smil10

EddyTheDog 10-17-2020 08:00 PM

Quote:

Originally Posted by Colmike7 (Post 22755115)
There's a java joke in here somewhere.. :food-smil10

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

sarettah 10-17-2020 08:09 PM

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


Dammit.

.

EddyTheDog 10-17-2020 08:31 PM

Quote:

Originally Posted by sarettah (Post 22755140)
Just wait till i'm in charge around here. You are all going to regret making fun of me.


Dammit.

.

Sorry - :pimp...

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

sarettah 10-17-2020 09:10 PM

Quote:

Originally Posted by EddyTheDog (Post 22755149)
Sorry - :pimp...

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.

.

Spunky 10-17-2020 09:21 PM

Ballers hire people to do that

EddyTheDog 10-17-2020 09:37 PM

Quote:

Originally Posted by Spunky (Post 22755171)
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...

Spunky 10-17-2020 09:41 PM

Quote:

Originally Posted by EddyTheDog (Post 22755185)
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 :2 cents:

EddyTheDog 10-17-2020 10:12 PM

Quote:

Originally Posted by Spunky (Post 22755186)
Mr Pheer wipes his ass with dolla bills :2 cents:

I wipe my ass with your 2 cents...

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

Spunky - You apolligise as well...

shake 10-17-2020 10:51 PM

this reminds my why i switched to nodejs from php, makes this like this much simpler

Mr Pheer 10-17-2020 11:40 PM

Quote:

Originally Posted by shake (Post 22755217)
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.

Mr Pheer 10-17-2020 11:45 PM

Quote:

Originally Posted by sarettah (Post 22755166)
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"

sarettah 10-18-2020 12:00 AM

Quote:

Originally Posted by Mr Pheer (Post 22755234)
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?

.

k0nr4d 10-18-2020 12:40 AM

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

https://media1.giphy.com/media/94EQmVHkveNck/source.gif

EddyTheDog 10-18-2020 01:06 AM

Quote:

Originally Posted by k0nr4d (Post 22755238)
No one ever said PHP was a good programming language...

https://media1.giphy.com/media/94EQmVHkveNck/source.gif

From you that's fucking hilarious!..

grumpy 10-18-2020 03:40 AM

Looks like json, maybe you should ask for all the data.

baddog 10-18-2020 11:29 AM

Quote:

Originally Posted by Colmike7 (Post 22755115)
There's a java joke in here somewhere.. :food-smil10

:1orglaugh:1orglaugh

ruff 10-18-2020 12:54 PM

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!

Colmike9 10-18-2020 01:00 PM

Quote:

Originally Posted by ruff (Post 22755457)
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. :upsidedow

I did COBOL for a little bit, too. Wasn't paying enough.

fuzebox 10-18-2020 04:21 PM

Quote:

Originally Posted by k0nr4d (Post 22755238)
No one ever said PHP was a good programming language...

https://media1.giphy.com/media/94EQmVHkveNck/source.gif

:pimp:pimp

sarettah 10-18-2020 04:29 PM

Quote:

Originally Posted by Colmike7 (Post 22755461)
Ugh, no thanks. In college I had to do Fortran 95 and Maple at the same time in one of the classes. It sucked. :upsidedow

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.

.

RyuLion 10-18-2020 05:33 PM

Quote:

Originally Posted by Colmike7 (Post 22755115)
There's a java joke in here somewhere.. :food-smil10

:1orglaugh Programmer character at its best! :1orglaugh

just a punk 10-19-2020 01:22 AM

Quote:

Originally Posted by Mr Pheer (Post 22754733)
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? :eek7

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 :pimp

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.

k0nr4d 10-19-2020 01:56 AM

Quote:

Originally Posted by CyberSEO (Post 22755651)
Only 3 lines of code. We call it optimization, bro :pimp

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 :1orglaugh

just a punk 10-19-2020 04:56 AM

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? ;)

k0nr4d 10-19-2020 05:06 AM

Quote:

Originally Posted by CyberSEO (Post 22755671)
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.

Mr Pheer 10-19-2020 08:29 AM

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

PornDude 10-19-2020 12:35 PM

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