how to remove a special character from a string and put a space in php code example

Example 1: php strip out special characters

function clean($string) {
   $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.

   return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}

Example 2: Remove Special Character in PHP

phpCopy<?php
$mainstr = "<h2>Welcome to <b>PHPWorld</b></h2>";

echo "Text before remove: \n" . $mainstr;

echo "\n\nText after remove: \n" .
    str_ireplace(array('<b>', '</b>', '<h2>', '</h2>'), '',
    htmlspecialchars($mainstr));
?>

Tags:

Php Example