Q:

Merge two regexp in single one php

I need to know if there is any way to  merge two regular expression into a single regexp. Recently I had to make the following php code but I feel that there is a simplified way to achieve this without using multiple preg_replace tags. What I am trying to do is strip off &nbsp; &amp; &copy; etc.. and to remove all multiple spaces
$textinput = 'this is a test input       \'     """"""    """" @#$$%&*)_+!@#$%^&*)   123    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;456';

$var = preg_replace("/&#?[a-z0-9]{2,8};/i",'',$textinput)
$string = preg_replace('/\s+/', ' ', $var);

output
this is a test input ' """""""""" @#$$%&*)_+!@#$%^&*) 123 456

I am aware about the html_entity_decode function in php to strip the special characters off, well this just an example! How can I merge both of the regexp into a single one?
Thank you!

A:

This will do your two replacements in one efficient step (without losing the whitespace character):
$replaced = preg_replace('~(?:&#?[a-z0-9]{2,8};)+|\s\K\s+~', '', $yourstring);

On the demo, see how all the extra characters are targeted.
Explanation

On the left side of the |, (?:&#?[a-z0-9]{2,8};)+ targets groups such as &nbsp;, not just one at a time but several together if they are touching.
On the right side, the \s matches one space, then the \K tells the engine to drop it from the match (it will not be replaced), then the \s+ matches any whitespace chars that follow
We replace with the empty string. 

