Ganesh Rathinavel
Ganesh Rathinavel

Reputation: 1335

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   & © 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);

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!

Upvotes: 2

Views: 74

Answers (3)

Avinash Raj
Avinash Raj

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    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

zx81
zx81

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

  • 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.

Upvotes: 2

Jason Larke
Jason Larke

Reputation: 5599

$var = preg_replace_callback('/&#?[a-z0-9]{2,8};|\s+/i', function($match) {
    return $match[0][0] === '&' ? '' : ' ';
}, $textinput);

Upvotes: 2

Related Questions