How to add / to preg_replace template

I made this simple function to filter data. I add characters that I allow to include, but I don’t know how to add the / character, as well

public function filter($text) { return preg_replace('/[^^a-zA-Z0-9#@:_(), .!@ " ]/','',$text); } 
+6
source share
2 answers

You can either escape with a backslash:

 preg_replace('/\//' ...); 

Or use other characters as delimiters:

 preg_replace('|/|' ...); 
+18
source

Just avoid the backslash character.

 public function filter($text) { return preg_replace('/[^^a-zA-Z0-9#@:_(), .!@ "\/ ]/','',$text); } 
+2
source

Source: https://habr.com/ru/post/912328/


All Articles