Để in ký tự không mong muốn, dùng preg_match_all(). Giả sử chuỗi có ký tự đặc biệt:
$sentence = "M-y Name/i_s John+Doe";
Muốn chỉ hiển thị ký tự đặc biệt:
-/_+
Ví dụ
<!DOCTYPE html> <html> <body> <?php $sentence = "M-y Name/i_s John+Doe"; echo "The original value is=", $sentence, "
"; if (preg_match_all('/[^a-zA-Z ]/', $sentence, $output)) { echo "The unwanted characters are as follows= " . implode('', $output[0]); } ?> </body> </html>
Kết quả
The original value is=M-y Name/i_s John+Doe The unwanted characters are as follows= -/_+