PHP
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.
}
Remove Special Character in PHP
phpCopy<?php
function RemoveSpecialChar($str)
{
$res = preg_replace('/[0-9@.;" "]+/', '', $str);
return $res;
}
$str = "My name is hello and email hello.world598@gmail.com;";
$str1 = RemoveSpecialChar($str);
echo "My UpdatedString: ", $str1;
?>
Remove special characters in php
function removeSpecialChar($string)
{
$string = strtolower($string);
$string = preg_replace('/[^da-z ]/i', '', $string);// Removes special chars.
$string = str_replace(' ', '-', $string); // Replaces all spaces with underscore.
return strtolower($string);
}
Remove Special Character in PHP
phpCopy<?php
$str = "@@HelloWorld";
$str1 = substr($str, 1);
echo $str1 . "
";
$str1 = substr($str, 2);
echo $str1;
?>
Remove Special Character in PHP
phpCopy<?php
$mainstr = "<h2>Welcome to <b>PHPWorld</b></h2>";
echo "Text before remove:
" . $mainstr;
echo "
Text after remove:
" .
str_ireplace(array('<b>', '</b>', '<h2>', '</h2>'), '',
htmlspecialchars($mainstr));
?>
Remove Special Char PHP
<?php
function RemoveSpecialChar($str)
{
$res = preg_replace('/[0-9@&^\%()#$!][}{*'".;" "]+/', ' ', $str);
return $res;
}
$str = "My name is hello and)(**&&^%$$#@! em[ail he}ll'o.world{59}8@gmail.com;";
$str1 = RemoveSpecialChar($str);
echo $str1;
?>
Remove Special Character in PHP
phpCopy<?php
$mainstr = "This is a sim'ple text;";
echo "Text before remove:
" . $mainstr, "
";
$replacestr = remove_sp_chr($mainstr);
function remove_sp_chr($str)
{
$result = str_replace(array("#", "'", ";"), '', $str);
echo "
Text after remove:
" . $result;
}
?>
Remove Special Character in PHP
phpCopy<?php
$str = "geeks";
$str = ltrim($str, 'g');
echo $str;
?>
Remove Special Character in PHP
phpCopy<?php
$mainstr = "@@PHP@Programming!!!.";
echo "Text before remove:
" . $mainstr;
echo "
Text after remove:
" . trim($mainstr, '@!.');
?>
Remove Special Character in PHP
phpCopy<?php
$string = "DelftStack is a best platform.....";
echo "Output: " . rtrim($string, ".");
?>
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 "
" . strtr($strTemplate, $strParams) . "
";
echo "
" . str_replace(array_keys($strParams), array_values($strParams), $strTemplate) . "
";
?>
Remove Special Character in PHP
phpCopy<?php
$str = "ei all, I said eello";
//$trans = array("h" => "-", "hello" => "hi", "hi" => "hello");
echo "Output: " . strtr($str, "e", "h");
?>