help php | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

help php

How to remove in string all characters except letters, numbers, and spaces?

9th Sep 2022, 6:40 PM
Анатолий Шмель
2 Answers
+ 5
use regex : //String containing special characters. $str = "this is- a' (test*&()"; //Remove any character that isn't A-Z, a-z, 0-9 or a whitespace. $str = preg_replace("/[^A-Za-z0-9 ]/", '', $str);
9th Sep 2022, 7:23 PM
Roland
Roland - avatar
+ 2
If the final goal is to prevent xss attack, you can use htmlspecialchars methode to convert special characters to HTML entities.
9th Sep 2022, 10:43 PM
Roland
Roland - avatar