A thug was taken into custody after he hurled a homophobic slur at another man before slashing him on a Manhattan train early Sunday, police sources said.

The 23-year-old victim was riding a Bronx-bound D train at the 125th Street station in Harlem around 5 a.m. when his attacker called him an “ugly f—-t” before slashing him in the left thigh, the sources said.

The victim was taken to St. Luke’s Hospital, where he is in stable condition.

The 39-year-old suspect fled, but police caught him at 127th Street and St. Nicholas Avenue. Charges are pending.

The knife was also recovered.