Q:

replace wrong html entities using preg_replace

unfortunately, I have to deal with faulty HTML entities in a text, for example

&middot,

instead of the correct entity with the ending semicolon.
I tried 
$pattern = "/^&[a-zA-Z]+?,/";
$replace =  "/^&[a-zA-Z]+?;/";
$detailtext = preg_replace($pattern,$replace, $detailtext);

but it doesn't work... This is driving me nuts, I'm new to regular expressions and any help would be greatly appreciated!
Example for $detailtext:
$detailtext = "Unterputz-Einsätze<br>mit Federklemmen (Verbindungsklemmen nach VDE 0632). <br>Die Einsätze können wahlweise mit Standard- oder Flächenabdeckung <br>bestückt werden.<br>Wippschalter<br>10 AX 250 V&sim,<br>Ausführung: Universalschalter (Aus-Wechsel)<br>"

A:

You should try this:
$pattern ="/(&[a-zA-Z]+),/"
$replace ="$1;";
$detailtext = preg_replace($pattern,$replace, $detailtext);

The parenthesis will capture entities that are followed by "," and you can retrieve them with "$n", n being the capture group number. So replace with "$1" followed with ending semicolon.

