Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
luckily, there's a handy array_filter function, that takes in an array, and removes all emp... oh, wait, no, it removes all FALSY items, so also say goodbye to your "0" and "false" items, if you had any.
-
MM8312274yThey probably fixed this with a different method, but left the broken one in there for someone to find and fix all over again later. PHP is that mental old woman who hasn't thrown anything out since 1988 and has to fight her way through piles of rubbish to get to her own toilet.
-
That's expected behaviour.
And as far as I know, it's the same behaviour in any language...
Just apply trim (or strip in Python) before exploding....
?!!!! -
Hazarth95014ywhat are you talking about? this is standard behaviour in pretty much every language I worked with... You can't leave dangling separators or it's gonna split them up even if the followup is an empty string
JavaScript:
"foo bar ".split(" ")
>result: Array(3) [ "hello", "world", "" ]
Python:
"foo bar ".split(" ")
>result: ['foo', 'bar', '']
C#:
String[] arr = "Hello World ".Split(' ');
Console.WriteLine("[{0}]", string.Join(", ", arr));
>result: [foo, bar, ]
Actually, weirdly enough, the only language I found that doesn't do this is Java
"foo bar ".split(" ");
>result: ["foo", "bar"]
or
"foo bar ".split("\\s");
>result: ["foo", "bar"]
I'm gonna go on a limb here and say that the only reason why java does it is because it uses regex for splitting instead of simple character splitting
Tbh, I'm more surprised about Java than PHP
The standard way to handle this is to call trim() first... -
Now that is an outright wrong example.
But yes, the stdlib of PHP is fucked up in all imaginable ways.
Switch to Python or wrap each required stdlib function to abstract the inconsistencies and other subtleties away. -
What really killed me is that Lerdorf back then chose the names of standard functions so that they would distribute better across the hash buckets. But the ultimate level of stupidity was that he abused the string length as fucking hash function!
-
Every time I see a PHP rant I always drop by for the comments section above all else.
-
C0D4681384yLike virtually any language that has this ability, one must RTFM and understand the expected output.
explode(" ", trim(" this is a string Whoot "));
There problem solved, or you know, don't explode spaces and have shitty data. -
ThIs Is Da ZaMe in EvEry lAnG, but Yi fk php hueheuheheu
y'all make no fucking sense i swear 🤣🤣🤣🤣 -
hjk10157314y@Midnight-shcode so you blame array_filter that takes an actual function to do EXACTLY what you want for having a very convenient default functionality that does not solve your own fuck-up.
To top it off this is all perfectly documented.
Regarding your OP just about any language you take issue with. Perhaps you are broken and undocumented not PHP. -
cprn17394yI just realized you can't import code from a parent directory in Python without hacks unless you install it system* wide first. PHP's `require` ftw!
*) venv is still a system, just virtualised -
@cprn i know! And to import python in sub folder you need to create empty __ini__.py for fuckall
-
hjk10157314y@jak645 only if you need backwards compatibility to pre 2012 versions of python. Since 3.3 you have namespaced packages and don't need the file unless you really want to init something.
-
@AleCx04 c# which is still the least crappy language i know has a parameter to filter out the empty entries...
-
@C0D4 c# which is still the least crappy language i know has a parameter to filter out the empty entries...
-
@YADU c# which is still the least crappy language i know has a parameter to filter out the empty entries...
-
@Hazarth yeah well firstly what do i call when my separator is not a space?
secondly, you sneakily ignored that c# has a parameter to tell it to filter the empty ones out. not ideal, but much better. -
Hazarth95014y@Midnight-shcode my point still stands, this isn't a php issue exactly. It isn't even rare.
Also I didn't know about the C# flag! Good to know. Though im not using C# as much last couple of years. -
C0D4681384y@Midnight-shcode explode() does have a limit flag, so you could do something like
explode(" ", "this is a bad string ", -1);
which would output:
Array
(
[0] => this
[1] => is
[2] => a
[3] => bad
[4] => string
)
however you would need to know to expect that last element to be blank, theres some drawbacks using the limit flag if you dont know your data.
another means would be to use array_filter()
https://php.net/manual/en/...
Of array_walk() if you need to write your own sanitizer Due to php's falsly way of doing things
By doing so you can remove the blank elements of the array after you explode() it, bit of double handling, but since your intent is to not clean the data first, we need to do it after the fact now. -
@Midnight-shcode like @C0D4 said, "since your intent is not to clean the data first, we have to do it aft the fact now"
Language ain't the issue here baby girl.
Related Rants
-
rephiscorth38Everyone here ranting about a fucking missing semicolon. I can't remember the last time a missing semicolon wa...
-
CodesNotHot10-Laughed at Gitlab the other day -Accidentally dropped my db today. fuck karma
-
codeclod15When you have a super annoying problem that Google has been unable to help with... But you stumble upon a link...
man, fuck php.
fuck fucking php fuckitily fucking hard.
basically every fuckin stdlib function it has is fucked in some way.
can't even remember it anymore properly, but...
for example:
explode(" ", "some string like this ");
function to "split a string into array of strings using the first param as the boundary marker.
the above one would return
["some", "string", "like", "this", ""]
BECAUSE I'M FUCKING FUCKITTY SURE THAT WHEN THE LAST CHARACTERS OF THE STRING ARE EQUAL TO THE SEPARATOR STRING, EVERYONE WANTS TO GET BACK ONE EXTRA FUCKING USELESS EMPTY ITEM IN THE ARRAY.
because beyond the last wall of my house, there's another room of my house, you stupid Lerdorf fuck.
rant
fuck
php