php Remove special characters from string

In this post we will show you how to php Remove special characters from string, hear we will give you different method for Remove special characters from string.

hear we will use may method for remove “special characters” with function like str_replace, preg_replace, strtolower, preg_match and other methods.

Methos – 1 For php Remove special characters from string

This methos is very easy and simple for use and execute For php Remove special characters from a string.

function remove_special_characters($user_string) {

   // Replaces all spaces using hyphens.
   $user_string = str_replace(' ', '-', $user_string); 

   // Removes special chars wothout A to Z and 0 to 9.
   return preg_replace('/[^A-Za-z0-9\-]/', '', $user_string); 

}

// call to function remove_special_characters with string
echo remove_special_characters('a|"btestc!@£de^&$f g');

Methos – 2 For php Remove special characters from string

This methos is very easy and simple for use and execute For php Remove all special characters from a string.

function remove_special_characters($user_string) {
	// Replaces all spaces with hyphens.
    $user_string = str_replace(' ', '-', $user_string); 
	
	// Removes special chars.
    $user_string = preg_replace('/[^A-Za-z0-9\-]/', '', $user_string); 

	// Replaces multiple hyphens with single one.
    return preg_replace('/-+/', '-', $user_string); 
}

// call to function remove_special_characters with string
echo remove_special_characters('a|"btestc!@£de^&$f g');

Methos – 3 For php Remove special characters from string

function remove_special_characters($user_string){
    $user_string = strtolower($user_string);
    $user_string = preg_replace('/[^a-z0-9 -]+/', '', $user_string);
    $user_string = str_replace(' ', '-', $user_string);
    return trim($user_string, '-');
}

// call to function remove_special_characters with string
echo remove_special_characters('a|"btestc!@£de^&$f g');

Methos – 4 For php Remove special characters from string

function remove_special_characters($user_string){

    $user_string = str_replace(array('[\', \']'), '', $user_string);
	
    $user_string = preg_replace('/\[.*\]/U', '', $user_string);
	
    $user_string = preg_replace('/&(amp;)?#?[a-z0-9]+;/i', '-', $user_string);
	
    $user_string = htmlentities($user_string, ENT_COMPAT, 'utf-8');
	
    $user_string = preg_replace('/&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);/i', '\\1', $user_string );
	
    $user_string = preg_replace(array('/[^a-z0-9]/i', '/[-]+/') , '-', $user_string);
	
    return strtolower(trim($user_string, '-'));
}

// call to function remove_special_characters with string
echo remove_special_characters('a|"btestc!@£de^&$f g');

Methos – 5 For php Remove special characters from string

function remove_special_characters($user_string)
{
	// Removes special chars wothout A to Z and 0 to 9.
	$user_string = preg_replace("/[^a-zA-Z0-9\s]/", "", $user_string);

	// Then changes spaces for unserscores
	$user_string = preg_replace('/\s/', '-', $user_string);

	// Finally encode it ready for use
	$user_string = urlencode($user_string);

}

// call to function remove_special_characters with string
echo remove_special_characters('a|"btestc!@£de^&$f g');

Methos – 6 For php Remove all special characters from string

$user_string = 'a|"btestc!@£de^&$f g';
echo preg_match('/'.preg_quote('^\'£$%^&*()}{@#~?><,@|-=-_+-¬', '/').'/', $user_string);

Methos – 7 For php Remove special characters from string

$user_string = 'a|"btestc!@£de^&$f g';

if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $user_string))
{
	// one or more 'special characters' found in $user_string
	echo preg_match('/'.preg_quote('^\'£$%^&*()}{@#~?><,@|-=-_+-¬', '/').'/', $user_string);    
}

Methos – 8 For php Remove special characters from string

function remove_special_characters($user_string) {
    return 
    ## strtolower(
          preg_replace(
            array('#[\\s-]+#', '#[^A-Za-z0-9\. -]+#'),
            array('-', ''),
        ##     cleanString(
              urldecode($user_string)
        ##     )
        )
    ## )
    ;
}

print implode("\n", array_map(
    function($user_string) {
            return $user_string . ' => ' . remove_special_characters($user_string);
    },
    array(
    'EVER%20gonna%20show%20you%20there',
    "I'm not the man I was",
    "'Légeresse' - dit sa majesté - adiós",
    )));

You also like google recaptcha using javascript and google recaptcha using php and PDO using Prepared Statements a Complete Reference and PHP Create Instagram location Image Fetcher