How do i remove special characters from a string in html?

I'd like to use any php function or whatever so that i can remove any HTML code and special characters and gives me only alpha-numeric output

$des = "Hello world) (*&^%$#@! it's me: and; love you.

";

I want the output become Hello world it s me and love you (just Aa-Zz-0-9-WhiteSpace)

I've tried strip_tags but it removes only HTML codes

$clear = strip_tags($des); 
echo $clear;

So is there any way to do it?

How do i remove special characters from a string in html?

Martijn

15.5k4 gold badges36 silver badges66 bronze badges

asked Aug 20, 2011 at 1:03

How do i remove special characters from a string in html?

Reham FahmyReham Fahmy

4,80315 gold badges49 silver badges70 bronze badges

Probably better here for a regex replace

// Strip HTML Tags
$clear = strip_tags($des);
// Clean up things like &
$clear = html_entity_decode($clear);
// Strip out any url-encoded stuff
$clear = urldecode($clear);
// Replace non-AlNum characters with space
$clear = preg_replace('/[^A-Za-z0-9]/', ' ', $clear);
// Replace Multiple spaces with single space
$clear = preg_replace('/ +/', ' ', $clear);
// Trim the string of leading/trailing space
$clear = trim($clear);

Or, in one go

$clear = trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', urldecode(html_entity_decode(strip_tags($des))))));

answered Aug 20, 2011 at 1:07

9

Strip out tags, leave only alphanumeric characters and space:

$clear = preg_replace('/[^a-zA-Z0-9\s]/', '', strip_tags($des));

Edit: all credit to DaveRandom for the perfect solution...

$clear = preg_replace('/[^a-zA-Z0-9\s]/', '', strip_tags(html_entity_decode($des)));

answered Aug 20, 2011 at 1:06

How do i remove special characters from a string in html?

Matt SteinMatt Stein

3,0441 gold badge18 silver badges34 bronze badges

1

All the other solutions are creepy because they are from someone that arrogantly simply thinks that English is the only language in the world :)

All those solutions strip also diacritics like ç or à.

The perfect solution, as stated in PHP documentation, is simply:

$clear = strip_tags($des);

answered Feb 22, 2017 at 21:49

1

In a more detailed manner from Above example, Considering below is your string:

$string = '
This..
is hello world ! هذا هو مرحبا العالم! !@#$%^&&**(*)<>?:";p[]"/.,\|`~1@#$%^&^&*(()908978867564564534423412313`1`` "Arabic Text نص عربي test 123 و,.m,............ ~~~ ٍ،]ٍْ}~ِ]ٍ}"; ';

Code:

echo preg_replace('/[^A-Za-z0-9 !@#$%^&*().]/u','', strip_tags($string));

Allows: English letters (Capital and small), 0 to 9 and characters !@#$%^&*().

Removes: All html tags, and special characters other than above

answered Dec 24, 2013 at 12:44

How do i remove special characters from a string in html?

Aditya P BhattAditya P Bhatt

20.6k17 gold badges81 silver badges103 bronze badges

2

You can do it in one single line :) specially useful for GET or POST requests

$clear = preg_replace('/[^A-Za-z0-9\-]/', '', urldecode($_GET['id']));

answered Feb 6, 2014 at 20:08

nodwsnodws

92013 silver badges18 bronze badges

Here's a function I've been using that I've put together from various threads around the net that removes everything, all tags and leaves you with a perfect phrase. Does anyone know how to modify this script to allow periods (.) ? In other words, leave everything 'as is' but leave the periods alone or other punctuation like and ! or a comma? let me know.

function stripAlpha( $item )

{

    $search     = array( 
         '@]*?>.*?@si'   // Strip out javascript 
        ,'@]*?>.*?@siU'    // Strip style tags properly 
        ,'@<[\/\!]*?[^<>]*?>@si'            // Strip out HTML tags
        ,'@@'         // Strip multi-line comments including CDATA
        ,'/\s{2,}/'
        ,'/(\s){2,}/'

    );

    $pattern    = array(

         '#[^a-zA-Z ]#'                     // Non alpha characters
        ,'/\s+/'                            // More than one whitespace

    );

    $replace    = array(
         ''
        ,' '

    );

    $item = preg_replace( $search, '', html_entity_decode( $item ) );
    $item = trim( preg_replace( $pattern, $replace, strip_tags( $item ) ) );
    return $item;

}

answered May 9, 2015 at 23:22

ViktorViktor

4973 silver badges21 bronze badges

1

to allow periods and any other character just add them like so:

change: '#[^a-zA-Z ]#' to:'#[^a-zA-Z .()!]#'

How do i remove special characters from a string in html?

sandip

5239 silver badges24 bronze badges

answered Jan 28, 2016 at 0:04

preg_replace('/[^a-zA-Z0-9\s]/', '',$string) this is using for removing special character only rather than space between the strings.

How do i remove special characters from a string in html?

answered Apr 25, 2017 at 4:34

Remove all special character don't give space write in single line

trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', 
urldecode(html_entity_decode(strip_tags($string))))));

How do i remove special characters from a string in html?

Pang

9,192146 gold badges85 silver badges118 bronze badges

answered Jul 16, 2017 at 22:39

How do i remove special characters from a string in html?

How do you remove special characters in HTML?

This should do what you're looking for: function clean($string) { $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens. return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars. } Hope it helpss!!

How do I remove special characters from a string?

Example of removing special characters using replaceAll() method.
public class RemoveSpecialCharacterExample1..
public static void main(String args[]).
String str= "This#string%contains^special*characters&.";.
str = str.replaceAll("[^a-zA-Z0-9]", " ");.
System.out.println(str);.

How do I remove a character from a string in HTML?

Using a replace() method with a regular expression To remove a character from a string, use string replace() and regular expression. This combination is used to remove all occurrences of the particular character, unlike the previous function.

How do I remove special characters and spaces from a string?

Use the replace() method to remove all special characters from a string, e.g. str. replace(/[^a-zA-Z0-9 ]/g, ''); . The replace method will return a new string that doesn't contain any special characters.