Reputation: 1335
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 & ©
etc.. and to remove all multiple spaces
$textinput = 'this is a test input \' """""" """" @#$$%&*)_+!@#$%^&*) 123 456';
$var = preg_replace("/&#?[a-z0-9]{2,8};/i",'',$textinput)
$string = preg_replace('/\s+/', ' ', $var);
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!
Upvotes: 2
Views: 74
Reputation: 174696
You could use a logical OR operator to combine both regexes,
(?:&#?[a-z0-9]{2,8};)+|(?<=\s)\s+
Your code would be,
<?php
$mystring = 'this is a test input \' """""" """" @#$$%&*)_+!@#$%^&*) 123 456';
$pattern = "~(?:&#?[a-z0-9]{2,8};)+|(?<=\s)\s+~";
$replacement = "";
echo preg_replace($pattern, $replacement, $mystring);
?>
OR
<?php
$mystring = 'this is a test input \' """""" """" @#$$%&*)_+!@#$%^&*) 123 456';
$pattern = "~&#?[a-z0-9]{2,8};|(?<=\s)\s+~";
$replacement = "";
echo preg_replace($pattern, $replacement, $mystring);
?>
output:
this is a test input ' """""" """" @#$$%&*)_+!@#$%^&*) 123 456
Upvotes: 2
Reputation: 41838
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
|
, (?:&#?[a-z0-9]{2,8};)+
targets groups such as
, not just one at a time but several together if they are touching.\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 followUpvotes: 2
Reputation: 5599
$var = preg_replace_callback('/&#?[a-z0-9]{2,8};|\s+/i', function($match) {
return $match[0][0] === '&' ? '' : ' ';
}, $textinput);
Upvotes: 2