How to trim special characters with PHP?

What is the best way to remove all non-alphanumeric characters from the beginning and end of a line?

~~STRING~~ => STRING "STRING" => STRING **STRING** => STRING **STR**ING** => STR**ING 
+1
source share
3 answers

You can use:

 $repl = preg_replace('#^\W+|\W+$#', '', $str) 
+3
source

trim($string, $charlist) should do the trick.

+2
source
 $str = preg_replace('/^\W*(.*\w)\W*$/', '$1', $str); 
+1
source

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


All Articles