php

How to remove special characters from a string in PHP

In this tutorial, we are going to see how to remove special characters from a string in PHP. You can use the str_replace() function in PHP to replace special characters in a string into an empty string.
 

How to remove special characters from a string in PHP
<?php
function deleteSpecialChar($str) {
      
    // replace all special characters by empty string 
    $res = str_replace( array( '%', '@', '\'', ';', '<', '>' ), ' ', $str);
      
    return $res;
}
  
// string example
$str = "A % B @ C <D>'E;"; 
  
// Call the function
$res = deleteSpecialChar($str); 
  
// Display the result
echo $res; 
?>

Output:

A B C D E

 
[st_adsense] mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *