Jump to content

.josh

Staff Alumni
  • Posts

    14,780
  • Joined

  • Last visited

  • Days Won

    43

Everything posted by .josh

  1. Then IMO you should look into using AJAX. Do you use a framework like jQuery?
  2. I don't understand why you are trying to disable the button...it's the button that allows the user to submit the form, yes? The button sort of has to work in order for the user to submit the form...
  3. It's not posting because you disabled the button. It's like cutting the wires between the switch and battery and then wondering why the lightbulb doesn't light up when you flip the switch... if you're lookin' to post the form w/out refreshing the page, then use ajax.
  4. Admittedly it's somewhat hard for me to understand what you're problem *really* is, since you are using random *labels* to describe your problem and putting them in *stars* instead of actually defining the problem. But I *think* you are saying you only want your form to populate the fields with the user-inputted values if it passes your validation, but your form fields are being populated even when the data does not pass validation? If this is the case, then solely based off the code you have provided, my guess is you have Register Globals turned on. If this is the case, your $_POST['firstName'] is automatically being assigned to $firstName upon form submission.
  5. I don't think the ++ is necessary though...it shouldn't ever backtrack because there's nothing else in the pattern to match for. Also, to 1up you on using something like trim to get rid of the @...can use \K to have the regex engine ditch it: @\K[^:]+
  6. IMO the "guideline" is the marks/comments so that you will improve next time around. Remember, school is a learning process, where you go from suck to not suck. It's not intended to be perfection every round. Also, it is a good thing to an extent to defend your work, but in the real world...you're always going to have a boss who tells you one thing and means another, and "That's not what you said" only goes so far. So, you should take this as a lesson to take it upon yourself to ask questions and get clarification on everything beforehand. Remember, the reason you (will) have the job is because you know the details, not the boss - they don't know any better, so it is up to you to pry that shit out of them. But anyways, yeah I agree as far as commenting. Always err on the side of over-commenting. If it's your own code that only you will ever touch, and you're confident in yourself that your naming conventions are enough for you, that's fine. But in the real world where you're working with lots of other peoples' code and they yours, there's really no such thing as too many comments. Also want to mention that when you comment, you need to comment what your intentions are, what you are expecting to happen. The code reflects the comments, not the other way around. IOW the comments are there to explain in human language what the script is supposed to be doing. For me, my pseudo-code usually turns into my comments. But also, comments are especially important if you're trying to track down a logic bug. The first and most important step to tracking down bugs is to figure out what it's supposed to be doing in the first place, so if you have clearly written comments then you can compare them to the actual code and more easily find those bugs. "Oh hey, it says here that I'm supposed to be doing xyz but the code clearly doesn't do that!" sort of thing.
  7. Okay so here is a tl;dr.... First off, a couple things to understand: To clarify preg_match() return values: With no 3rd argument, preg_match() returns integer value of 1, 0 or boolean value of false. "false" only happens if it returns an error from parsing (like you forgot a delimiter or something) so for these examples, the "false" output is never because of this, because the syntax is correct. IOW in these examples, preg_match() itself always returns an integer value of 1 or 0. 2nd point is that ! is the logical NOT operator. It returns a boolean false if the expression evaluates true, and a boolean true if the expression evaluates false. 1 evaluates true and 0 evaluates false. So for instance, if preg_match were to return an integer of 1 because it matched something but you have a ! in front of it, 1 is true so the ! will return a boolean false. 3rd point is that two of these are "right", if you put your "give error" code vs. "perform query" code in the right place based on the logic. But one of them is more clear, based on how people read things in general, or how the rest of your code is setup. So even though there are multiple "right answers" per se, you should pick the version that clearly conveys your intentions and makes the code easiest to read. Also, there are two wrong answers here, which are demonstrated in the code examples below. 4th point is that when I refer to "valid" or "invalid" string (the posted username), there are 2 different contexts: 1) what YOU intend to be valid (a username with only letters or numbers, 5 to 20 chars long), and 2) what the regex pattern considers an (in)valid match. Hopefully you can see from the context what I mean, and I also try to be explicit for the most part. This is important to understand, so that you can understand why the two wrong answers are wrong. So.... based on those things, let's look at the examples 1) positive char class with logical NOT operator The regex matches for 5 to 20 numbers or letters, and the NOT operator inverts the result var_dump(!preg_match('/^[A-Za-z0-9]{5,20}$/','12345')); // output: bool (false) var_dump(!preg_match('/^[A-Za-z0-9]{5,20}$/','123')); // output: bool (true) var_dump(!preg_match('/^[A-Za-z0-9]{5,20}$/','.....')); // output: bool (true) '12345' is valid to you AND to the regex (right chars, right length). preg_match() returns an integer value of 1, which evaluates true, but the ! operator inverts that and returns a boolean false. '123' is invalid to the regex AND to you (right chars, wrong length). preg_match() returns an integer value of 0, which evaluates false, but the ! operator inverts that and returns a boolean true. '.....' is invalid to the regex AND to you (wrong chars, right length). preg_match() returns an integer value of 0, which evaluates false, but the ! operator inverts that and returns a boolean true. The following codeblock will technically work, but it is not very clear, because it's evaluating "true" on what you consider to be invalid usernames : if ( !preg_match('/^[A-Za-z0-9]{5,20}$/',$_POST['Username']) ) { // will execute if username '123' // will execute if username '.....' } else { // will execute if username '12345' } 2) negative char class with logical NOT operator The regex matches for 5 to 20 of anything NOT a number or letter, and the NOT operator inverts the result. var_dump(!preg_match('/^[^A-Za-z0-9]{5,20}$/','12345')); // output: bool (true) var_dump(!preg_match('/^[^A-Za-z0-9]{5,20}$/','123')); // output: bool (true) var_dump(!preg_match('/^[^A-Za-z0-9]{5,20}$/','.....')); // output: bool (false) '12345' is valid to you (right chars, right length) but invalid to the regex (wrong chars, right length). preg_match() returns an integer value of 0, which evaluates false, but the ! operator inverts that and returns a boolean true. '123' is invalid to you (right chars, wrong length) and invalid to the regex (wrong chars, wrong length). preg_match() returns an integer value of 0, which evaluates false, but the ! operator inverts that and returns a boolean true. '.....' is invalid to you (wrong chars, right length) but valid to the regex (right chars, right length). preg_match() returns an integer value of 1, which evaluates true, but the ! operator inverts that and returns a boolean false. This will not work in an if..else because your expectations and the regex's expectations do not line up 100%, so you will end up with false positives! // bad! if ( !preg_match('/^[^A-Za-z0-9]{5,20}$/',$_POST['Username']) ) { // will execute if username is '12345' // will execute if username is '123' } else { // will execute if username is '.....' } 3) Positive char class without logical NOT operator The regex matches for 5 to 20 numbers or letters var_dump(preg_match('/^[A-Za-z0-9]{5,20}$/','12345')); // output: int (1) var_dump(preg_match('/^[A-Za-z0-9]{5,20}$/','123')); // output: int (0) var_dump(preg_match('/^[A-Za-z0-9]{5,20}$/','.....')); // output: int (0) '12345' is valid to you and the regex (right chars, right length). preg_match() returns an integer value of 1, which evaluates true '123' is invalid to you and the regex (right chars, wrong length). preg_match() returns an integer value of 0, which evaluates false '.....' is invalid you and the regex (wrong chars, right length). preg_match() returns an integer value of 0, which evaluates false The following codeblock will work, and the intention is clear: "if the username is valid, do this..otherwise do this" if ( preg_match('/^[A-Za-z0-9]{5,20}$/',$_POST['Username']) ) { // will execute if username '12345' } else { // will execute if username '123' // will execute if username '.....' } 4) negative char class without logical NOT operator The regex matches for 5 to 20 of anything NOT a number or letter var_dump(preg_match('/^[^A-Za-z0-9]{5,20}$/','12345')); // output: int (0) var_dump(preg_match('/^[^A-Za-z0-9]{5,20}$/','123')); // output: int (0) var_dump(preg_match('/^[^A-Za-z0-9]{5,20}$/','.....')); // output: int (1) '12345' is valid to you (right chars, right length) but invalid for the regex (wrong chars, right length). preg_match() returns an integer value of 0, which evaluates false '123' is invalid to you (right chars, wrong length) and invalid for the regex (wrong chars, wrong length). preg_match() returns an integer value of 0, which evaluates false '.....' is invalid you (wrong chars, wrong length) but valid for the regex (right chars, right length). preg_match() returns an integer value of 1, which evaluates true This will not work in an if..else because your expectations and the regex's expectations do not line up 100%, so you will end up with false negatives! // bad! if ( preg_match('/^[^A-Za-z0-9]{5,20}$/',$_POST['Username']) ) { // will execute if username is '.....' } else { // will execute if username is '12345' // will execute if username is '123' }
  8. If only he said sudo make me a sammich...
  9. if (!preg_match('/^[^A-Za-z0-9]{5,20}$/',$_POST['Username']) ! This means the inverse of what is returned from preg_match(). preg_match() without a 3rd argument will return true if a match occurred, false if no match. So putting the ! in front of it inverses that for the "if(...)" condition. So if preg_match() returns true (match occurred), and you have ! in front of preg_match, the expression inverts that to false, so the condition evaluates false. ^ outside of the [...] this signifies start of string anchor tag (or start of line if you use a modifier, but that's another can of worms). This is, coupled with the $ (end of string/line counterpart) ensures that the match is being performed on the whole string, not just a substring within the string. ^ The [...] brackets is called a character class. It's basically a list of characters to match for. Having a ^ at the start [^...] means to match anything NOT within the [...] brackets (which makes it a negative character class).
  10. http://www.thinkgeek.com/geek-kids/newborn-infant/ http://www.jinx.com/shop/g/baby/
  11. I tend to agree. I see the same types of questions/problems/people at my job as I do here, so it's no stretch of the imagination to assume that some % of our population is more than random hobbyists. Also, why else would we constantly get "please delete my thread/account, I don't want my boss/client to ever find this" threads?
  12. Just wanna throw out there that if you're simply looking to see if $_POST['name'] only has letters or numbers and you there is no length limit etc.. you don't even need to use regex, just use ctype_alnum if (ctype_alnum($_POST['name'])) { // it's good, do something } else { // it's bad, do something }
  13. .josh

    preg_match_all

    Also you're probably going to run into trouble with those greedy quantifiers...
  14. As someone who writes technical manuals and docs for a living, I advise you to take the time to read it, even if it's a lengthy read. For sure, not all 1k pages are relevant to this situation, but I'm positive they are all relevant to a situation. No doubt one of the best ways to improve yourself as a programmer is to RTFM
  15. Hello jondoe, A few more notes: - People bump their threads after X time to move the thread back to the top of the list, so that people can see the thread. This is especially useful if you do not get a response for a while and the thread moves to the next page. You are allowed to do this, within reason, and in fact, we prefer this over making a new thread asking the same thing just to get it back on top (also note: We will issue warning for double posting, but not bumping threads if it is within reason). If you edit a post, it does not refresh the thread position, so being able to edit posts will do nothing for bumping threads. Rephrasing for clarification is good and all, but if the thread remains on page 3 or w/e, your efforts would be fruitless. Therefore, bumping a thread with a followup post is the best method. - If a thread diverges to another subject worthy of being in a different forum (or even if it's within the same forum), the best course of action would be to make a new thread that stands on its own. If you have another question, even if it's related but not the same, make a new thread! - By far most "edit my post" requests have to do with removing something. For instance, look at this very thread: the OP asked for help a while back and included his website info, and has asked for those references to be removed. Actually it's usually not something specific like website links or passwords; it's usually "delete my whole post/thread". And 99% of the time the reason is "I don't want clients/customers/bosses being able to find out I had to ask a question somewhere." You aren't even making a case for requests like these, but to restate what thorpe said, we usually do not honor them because we have rules clearly posted that we do not delete content, even in red letters above all post buttons.
  16. lol that would be cool, I haven't gotten to try minecraft yet
  17. In general you shouldn't be aiming to simply have different translations of your pages, but aim to have localized versions of the pages. But even still, it will not hurt you to have separate URLs for each translation, because the search engine algorithms will not really put them in the same bucket at a higher level. When you do a search for "apple" do you see results popping up in spanish? Same principle. You will have your english version page competing against other english key-word pages, and your spanish version competing against spanish keywords, etc... and Adam makes a good suggestion for how to structure your URLs for it. But while we're on the subject of SEO...you should work on making your URLs more friendly than ".../posts/21". I'm gonna assume that you have some sort of CMS like WordPress and those are blog/article posts... most CMSs have built-in the ability to set the URLs to be more SEO friendly than having an ID like that. Basically they will take the title of the blog/article and use that, or some other value you specify, so that you get something more like "mysite.com/top-10-things-you-never-knew.html" or something. This is 100x better than having an ID in the URL that means nothing to crawlers. And if your site doesn't have something like that built-in, then you really need to invest in doing it. Here is a link to some SEO best-practices, straight from the horse's mouth: http://static.googleusercontent.com/external_content/untrusted_dlcp/www.google.com/en/us/webmasters/docs/search-engine-optimization-starter-guide.pdf
  18. I assume this is in regards to your Almost there... thread. From that thread, this is what I see: You made a post asking for help (but did a poor job asking the question, see my response in your thread). We have forum stickies for a reason. Contrary to the belief of some people, they are not there so that we can be assholes on power trips. They are there because we know from a lot of experience trying to help people that we need certain info in order to help with a problem, and that usually when a person asks for help, they rarely know how to even ask for that help. You PMed Xyph asking for help, which is against the rules. You defended your PM to Xyph by saying it is frustrating when nobody bothers to reply when you've had this problem for a week+ straight. First off, less than 24 hours passed between your OP and Xyph's response, so you PMed him sometime in between. Whether or not you've had this problem for a week+ we don't know; as far as we're concerned, your problem started when you made the thread, so you waited less than 24 hours before you PMed Xyph, not a week+ (not that it would make it any more okay to PM someone even if it were a week+). Whether or not you choose to read the rules is on you, but they are enforced just the same, and not knowing them is not an excuse. We have them clearly posted and even have explicitly added a notice and link in red, right above every Post/Preview button on the board. You also responded by asking a basic question that could have easily been googled. Yes, you have a "right" to ask questions (insofar as that our terms give you this right...this is a privately owned site), but if you're relying on a free service to ask questions, it is common knowledge that free services do not cater to questions that can easily be found on your own. Relevant results appear on the first page of google even with just the term "quantifier", not even throwing in other keywords like "regex" "php" "programming" etc... ...and should you decide that we're a bunch of elitists for having this mentality, I invite you to spend a week+ in our shoes before making that judgment. Offer your own expertise(s) for free and see how long it takes you to start trying to filter out stuff. Use some common sense. It's like finding a magic lamp and then wishing for something stupid and common and easily obtainable that you totally didn't need to burn a wish for. But even still, we're not the internets' personal genie at beck and call, required to grant whatever comes out of peoples' mouth fappings. In summary, I see that you didn't bother to read the rules and consequently broke them, and didn't bother to read stickies and consequently asked your question poorly. And then you get all pissy because nobody bothered to respond, and when they did, they called you out on it. So after 2 years of expecting us to cater to your lazy rude ass, you have decided that we fail and are moving on. Well I'm *so* sorry to disappoint. You walked into our house and didn't bother to take your muddy shoes off, plopped yourself down on our couch and propped your feet up on our coffee table and started yelling at us to make you a sammich....fuck off.
  19. We have stickies for a reason. Contrary to the belief of some people, they are not there so that we can be assholes on power trips. They are there because we know from a lot of experience trying to help people that we need certain info in order to help with a problem, and that usually when a person asks for help, they rarely know how to even ask for that help. It's kind of hard to know what the problem really is when you didn't properly explain the problem in the first place. You show your regex, which is good. But then you point at two of the array entries and show a picture of a list. You did not explain what the problem actually is. My best guess is that you're saying the bullet points aren't lined up properly but it's only a guess. You failed to show the subject, the content you are trying to perform the regex on. Nor did you show what the preg_replaces are doing vs. what you want it do. Also, if the issue is that the bullets aren't lined up properly, it's probably not even a regex issue, but a styling issue (css).
  20. If I remember correctly, it got its start because a former admin had a math problem he had to work out, and for some reason felt it warranted its own forum. Hahaha those crazy admin of old! Whether or not we should keep the Math board is a subject that has actually come up several times. And there are people on both sides of the fence on it, but I guess it probably remains as-is because it's not technically hurting anything by being there. Also we're pretty lazy, so it's hard to motivate us to actually do anything around here. As for me, I don't particularly disagree with having a "Math" forum...having a basic understanding of math (like what, basic algebra, maybe trig tops? It's been decades since I was in school...) is necessary for everyday programming, and the forum does have more posts than a lot of our other forums...but I think if we do decide to keep that form, "Math" is programming language agnostic, so it doesn't make a whole lot of sense to make it php-only, and probably should be moved outside of php help. There are plenty of posts in that forum that don't mention php (or any language) at all. But that's assuming it even sticks around. As Nightslyr mentioned, this is primarily a php programming help board...even if math is language agnostic, we aren't primarily here to hand out math lessons.
  21. Um, no. We already know your intentions, so "restating" your question doesn't work. And you insulted a staff member on top of that...really? Since you aren't interested in following laws, is it fair to assume you aren't interested in following rules on our forum either? Should I just cut to the chase and ban you? Thread closed.
  22. http://static.googleusercontent.com/external_content/untrusted_dlcp/www.google.com/en/us/webmasters/docs/search-engine-optimization-starter-guide.pdf
  23. There is no built-in php function to parse javascript. It is a different language, simple as that. I believe I have seen some people attempt to make scripts that will parse javascript, but this is not really the same as actually executing it within a DOM environment. In order to get the code as you would see in a live DOM in a browser, you basically have to emulate a browser on your server. This is beyond the scope of php alone.
  24. ...well ajax is just one part of javascript...the page may not even have any ajax calls on it.
  25. You cannot do this with curl. You need to use curl to grab the page content initially and then run it through something that will parse it, like what a browser does.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.