Dùng preg_replace() với regex để xóa ký tự không phải alphanumeric.
Cú pháp
preg_replace(pattern, replacement, subject, limit, count)
Ví dụ
<?php
$str = "tu!tor^ials$%!poi&nt";
echo preg_replace('/[^a-zA-Z0-9]/', '', $str);
?>
Kết quả
tutorialspoint
Giải thích
Regex /[^a-zA-Z0-9]/ khớp mọi ký tự không phải chữ cái và số. Thay thế bằng chuỗi rỗng.