PHILADELPHIA — District Attorney R. Seth Williams pleaded guilty to bribery and quit his position as Philadelphia’s chief law enforcement officer on Thursday, bringing to an abrupt halt his trial on federal corruption charges.

The sudden plea came after some two weeks of testimony in which prosecutors alleged that Mr. Williams took cash and gifts from businesspeople in return for offers of legal help with their cases or those of their friends.

Mr. Williams, who was elected in 2009 as the city’s first black district attorney, now faces a prison term of up to five years and a fine of up to $250,000 when he is sentenced on Oct. 24. He is being held in custody until the sentencing.

Mr. Williams, 50, was charged in March and May with 29 counts of accepting bribes from businesspeople in exchange for offering to help them with their cases. His guilty plea was on one charge of travel and use of interstate facilities to facilitate bribery. Prosecutors will drop the remaining 28 charges under the terms of a plea agreement.