php strip special characters 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
$strTemplate = "My name is :name, not :name2.";
$strParams = [
    ':name' => 'Dave',
    'Dave' => ':name2 or :password',
    ':name2' => 'Steve',
    ':pass' => '7hf2348', ];
echo "\n" . strtr($strTemplate, $strParams) . "\n";
echo "\n" . str_replace(array_keys($strParams), array_values($strParams), $strTemplate) . "\n";


?>

Example 3: Remove Special Character in PHP

phpCopy<?php
$str = "@@HelloWorld";
$str1 = substr($str, 1);
echo $str1 . "\n\n";
$str1 = substr($str, 2);
echo $str1;
?>

Tags:

Php Example