It is written in Pirkei Avos "Let the poor be members of your household and don't talk much with women". R' Nachman says that the Mishna is hinting to us a secret, namely, that Tzedaka is a great s egulah not to come to sexual fantasies. Even so, continues R' Nachman, the Mishna is exhorting us not to talk much with women, because this segulah is only good to save a person from fantasies if he does not talk with women more than he needs to. And R' Nachman continues, that this is also the meaning of the words "Tzedaka Tatzil Mi'Maves" - "Tzedaka saves from death". For sexual fantasies, explains R' Nachman, are death itself.
On a practical level, if one commits (for a week at a time) to donate 25 cents to Tzedaka each time he lets himself dwell on a sexual fantasy, he will likely see good progress. Not only may this help psychologically to prevent him dwelling on such thoughts, but by donating the money to Tzedaka one will also be fulfilling the s egulah and thereby prevent further thoughts from coming as well!
While we are on the subject of Pirkei Avos, I want to quote from a comment that someone posted on an article about Internet Filters (on a Religious Web-site):
"Rabban Gamliel the son of Rabbi Yehuda the Prince said, good is Torah study together with a worldly occupation, for the exertion in both makes one forget sin". Also, a person is not allowed to injure himself. Watching pornography or sites that turn women into sexual objects is like swallowing poison.