Can you not do {min,max} in the lookbehind regex? ie, instead of this:
$entity = '(?<!&' . '[a-zA-Z0-9]{2}' . '|' . '#[0-9]{2}' . '|' . '[a-zA-Z0-9]{3}' . '|' . '#[0-9]{3}' . '|' . '[a-zA-Z0-9]{4}' . '|' . '#[0-9]{4}' . '|' . '[a-zA-Z0-9]{5}' . '|' . '[a-zA-Z0-9]{6}' . '|' . '[a-zA-Z0-9]{7}' . ')';
Do this: $entity = '(?<!&' . '[a-zA-Z0-9]{2,7}' . '|' . '#[0-9]{2,4}' . ')';
Or is that the assertion format restriction you mention in the comment?
-Brad
List info: http://lists.roundcube.net/dev/ BT/aba52c80