89
u/Jack126Guy May 04 '15
A bit further down:
$words = preg_replace('/A/', 'a', $words); // convert to lowercase
$words = preg_replace('/B/', 'b', $words); // convert to lowercase
$words = preg_replace('/C/', 'c', $words); // convert to lowercase
$words = preg_replace('/D/', 'd', $words); // convert to lowercase
// ...26
73
May 04 '15
Why so complicated? Just put it into one line:
$words = preg_replace('/(?:[^234567890\D]|[^134567890\D]|[^124567890\D]|[^123567890\D]|[^123467890\D]|[^123457890\D]|[^123456890\D]|[^123456790\D]|[^123456780\D]|[^123456789\D]){1,}/', '', $words);If I'd have more time I'd use negative lookarounds as that's the only way to be sure every digit is found and eliminated!
10
18
u/Meshiest May 04 '15
claps slowly
13
u/globalvarsonly May 05 '15 edited May 05 '15
while () { hands->clap; $edit++; sleep 3; }6
u/Meshiest May 05 '15
you're not using any global variables :c
6
u/globalvarsonly May 05 '15
Oops, $edit is now incremented in each loop, and is a global in package "main" by default in my shitty non-strict perl.
2
37
May 04 '15
I spent a lot of time wondering what the fuck were all those swords.
I'm not used to php.
15
u/ireddit_didu May 05 '15
$words for $200
8
17
u/WASDx May 04 '15
What a fool, he should have used a for loop.
9
u/greenwizard88 May 05 '15
Like this?
$letters = range('A', 'Z'); foreach($letters as $letter) { $lower = strtolower($letter); $words = preg_replace('/'.$letter.'/', ''.$lower.'', $words); } $numbers = range(0, 9); foreach($numbers as $number) { $words = preg_replace('/'.$number.'/', '', $words); }
16
u/Lopsidation May 05 '15
Wow, what terrible code. This only removes the one-digit numbers; he needs at least 2 billion more lines to get them all.
3
u/Chondriac May 06 '15
also some more comments. wouldn't want to have to scroll up 2 billion lines of code to remember what that line is doing.
14
9
u/TheWheez May 05 '15
Hey please don't hurt me for asking but I'm trying to figure out what the problem is here. Is it a PHP thing or just the fact that he's hardcoding this for every respective number?
18
u/recursive May 05 '15
The problem is that there are much better ways of doing this. If you use regular expression (he is), you can do it in one statement. If you're going to replace every character separately, you don't need the overhead of regular expressions.
7
u/TheWheez May 05 '15 edited May 05 '15
Ah I guess I didn't realize preg_replace was for regular expressions.
Thanks!
39
u/annoyed_freelancer May 04 '15
$words = preg_replace('/[0-9]/', '', $words);46
19
3
-11
May 04 '15
Why is it that programmers always feel the need to fix everything they see? That was precisely the first thing I've thought of when I saw the code.
46
u/bakmanthetitan329 May 04 '15
Not being a professional programmer, I can say that I've been confused several times as to what is wrong with some code on this sub. I always appreciate people using this sub constructively.
8
May 04 '15
I'm not saying it's a bad thing, it's just something I noticed. That was actually a genuine question.
6
u/IWentToTheWoods May 05 '15
I noticed something similar with FizzBuzz. The first articles to popularize it just mentioned it in passing, since the point of those articles was how many people applying for programming jobs couldn't write a simple program. Instead of addressing that topic, the comments were full of people saying "here's how I'd write it in $language!"
I suppose it's a form of nerd sniping, where none of us can concentrate when there's a puzzle at hand.
1
0
u/ababcock1 May 05 '15
If you find yourself copy pasting significant amounts of code you are almost certainly doing it wrong.
4
u/amazing_rando May 04 '15
I don't think it's specific to programmers. I write a lot on the side, and I find myself mentally correcting other people's grammar & editing for poor word choice about as often as I find myself trying to fix bad code.
I'm less likely to post it as a comment, though. I figure a lot of people use this subreddit as a learning source.
3
2
u/Felz May 04 '15
Professional programmer here- ugly code often hides bugs, so it's either fix it now or spend hours finding it was a problem and fixing it later. More importantly, elegance makes me happy.
0
5
2
2
3
u/seriouslulz May 04 '15
for ($i = 0; $i < 10; $i++) {
$words = str_replace(strval($i), '', $words);
}1
1
u/dsiOneBAN2 May 12 '15
How does one manage to learn just the right amount of regex to do this but not a better way?
8
u/MeowMeowWoofWoof May 04 '15
This causes me pain. Why am I even looking at this?