For the third time in two months, a federal judge has knocked down an immigration order by President Donald Trump — he vowed to appeal the "ridiculous" decision — and used Trump's own language against him in the ruling.

In a ruling on Tuesday, U.S. District Judge William Orrick quoted Trump to support his decision to block the president's order to withhold funding from "sanctuary cities" that do not cooperate with U.S. immigration officials.

Trump called the sanctuary cities order a "weapon" against communities that disagree with his preferred immigration policy, Orrick said. The judge also cited a February interview in which he said the president threatened to cut off funding to California, saying the state "in many ways is out of control."

The first comment was evidence that the administration intended the executive order to apply broadly to all sorts of federal funding, and not a relatively small pot of grant money as the Department of Justice had argued, the judge said.

The second statement showed the two California governments that sued to block the order — San Francisco and Santa Clara County — had good reason to believe they would be targeted, Orrick said.

The sanctuary city order was among a flurry of immigration measures Trump signed in January, including a ban on travelers from seven Muslim-majority countries and a directive calling for a wall on the Mexican border.

As the administration approaches its 100th day in office later this month, Orrick's ruling was another immigration policy setback, which Trump noted Wednesday morning in a tweet.

"First the Ninth Circuit rules against the ban & now it hits again on sanctuary cities-both ridiculous rulings. See you in the Supreme Court!" he wrote, incorrectly referring to the Ninth Circuit.

While Trump tweeted a similar vow to appeal to the Supreme Court over the Ninth Circuit ruling on his travel ban, his administration has not yet done so.

Trump's words were also cited by federal judges in Maryland and Hawaii, who last month blocked his revised ban on new visas for people from six Muslim-majority countries. U.S. District Judge Derrick Watson in Hawaii and U.S. District Judge Theodore Chuang in Maryland said comments by Trump supported the allegation that the ban was aimed at Muslims.

Orrick's preliminary injunction against the sanctuary cities order will stay in place while the lawsuits by San Francisco and Santa Clara work their way through court.

The government hasn't cut off any money yet or declared any communities sanctuary cities. But the Justice Department sent letters last week advising communities to prove they are in compliance. California was informed it could lose $18.2 million.

Orrick said Trump cannot set new conditions on spending approved by Congress.

Even if the president could do so, those conditions would have to be clearly related to the funds at issue and not coercive, as the executive order appeared to be, Orrick said.

White House chief of staff Reince Priebus described the ruling as another example of the "9th Circuit going bananas."

The administration has often criticized the 9th U.S. Circuit Court of Appeals. Orrick does not sit on that court but his district is in the territory of the appeals court, which has ruled against one version of Trump's travel ban.

"The idea that an agency can't put in some reasonable restriction on how some of these moneys are spent is something that will be overturned eventually, and we will win at the Supreme Court level at some point," Priebus said.

The Trump administration says sanctuary cities allow dangerous criminals back on the street and that the order is needed to keep the country safe. San Francisco and other sanctuary cities say turning local police into immigration officers erodes the trust that is needed to get people to report crime.

San Francisco City Attorney Dennis Herrera praised the ruling and said the president was "forced to back down."

"This is why we have courts — to halt the overreach of a president and an attorney general who either don't understand the Constitution or chose to ignore it," Herrera said in a statement.

Associated Press writers Sadie Gurman and Julie Bykowicz contributed to this story.