4 February 2024

If you want to remove certain patterns from a string in PHP using regular expressions, you can use the preg_replace function. Clone this snippet and replace "/pattern/" with the actual regular expression pattern you want to remove from your string. I have no affiliation and I recommend to use https://regex101.com/ to test your regular expressions.

Source code viewer
  1. // Your input string
  2. $inputString = "This is a sample string with some patterns to remove.";
  3.  
  4. // Define the pattern you want to remove
  5. $patternToRemove = '/pattern/';
  6.  
  7. // Use preg_replace to remove the pattern
  8. $outputString = preg_replace($patternToRemove, '', $inputString);
  9.  
  10. // Output the result
  11. echo $outputString;
Programming Language: PHP