PHP preg_replace regexp for dash

I am trying to replace something in a string that is not a letter, number or dash "-".

How to change this line to include a dash?

$link = preg_replace('/[^a-z0-9]/', "", strtolower($_POST['link_name'])); 

Am I just pasting it there?

 $link = preg_replace('/[^a-z0-9-]/', "", strtolower($_POST['link_name'])); 
+4
source share
2 answers

You need to exit - as it is a special character for regular expressions:

 $link = preg_replace('/[^a-z0-9\-]/', '', strtolower($_POST['link_name'])); 
+9
source

Just add - to the end of the class ( [^a-z0-9-] ).

- does not matter much at the end of the class. Alternatively, avoid it with a backslash.

+6
source

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


All Articles