Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strip out HTML and Special Characters

Tags:

php

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)<b> (*&^%$#@! it's me: and; love you.<p>"; 

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?

like image 650
Reham Fahmy Avatar asked Aug 20 '11 01:08

Reham Fahmy


People also ask

How do I 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!!

What does it mean to strip HTML?

stripHtml( html ) Changes the provided HTML string into a plain text string by converting <br> , <p> , and <div> to line breaks, stripping all other tags, and converting escaped characters into their display values.

How do I remove a string in HTML?

The HTML tags can be removed from a given string by using replaceAll() method of String class. We can remove the HTML tags from a given string by using a regular expression. After removing the HTML tags from a string, it will return a string as normal text.


2 Answers

Probably better here for a regex replace

// Strip HTML Tags $clear = strip_tags($des); // Clean up things like &amp; $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)))))); 
like image 150
Mez Avatar answered Sep 22 '22 14:09

Mez


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))); 
like image 30
Matt Stein Avatar answered Sep 23 '22 14:09

Matt Stein