Standard guidance lives here: http://stackoverflow.com/help/referencinghttps://stackoverflow.com/help/referencing
Do not copy the complete text of external sources; instead, use their words and ideas to support your own. And always give proper credit to the author and site where you found the text, including a direct link to it.
In other words, identify the relevant bits of code, quote them, and then explain in your own words why they're relevant and useful in solving the problem at hand.