How to Remove HTML Special Chars

How to remove html special chars?

Either decode them using html_entity_decode or remove them using preg_replace:

$Content = preg_replace("/&#?[a-z0-9]+;/i","",$Content); 

(From here)

EDIT: Alternative according to Jacco's comment

might be nice to replace the '+' with
{2,8} or something. This will limit
the chance of replacing entire
sentences when an unencoded '&' is
present.

$Content = preg_replace("/&#?[a-z0-9]{2,8};/i","",$Content); 

Strip out HTML and Special Characters

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))))));

Removing HTML elements with special characters

Instead of trying to escape the characters yourself you could try a couple of other ways:

  1. Use jQuery's escapeSelector() on the id string. This will escape any special characters in the string. Note escapeSelector was added in v3.0 of jQuery. View how they are doing the escaping here
    if interested.

    $( '#'+ $.escapeSelector('theText') )
  2. Use an attribute selector instead of trying to escape all the possible characters for an id selector

    $('[id="idHere"]')

    This however will select multiple elements if for some bizarre reason you have multiple elements with the same id.

Demo





var id = "some,weird®,id";

var id2 = "some,other®,id";


$('#'+ $.escapeSelector(id2) ).css({border:'1px solid green'});


$('[id="'+id+'"]').css({border:'1px solid red'});
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

<div id="some,weird®,id"></div>

<br/>

<div id="some,other®,id"></div>

Remove special characters from string except special character inside HTML tags

Don't do this but if you have to do it any way there's a workaround (not 100% guaranteed)





var str = "By: <span style='background-color:#ffc8c4;'>Anita</span> <span style='background-color:#ffc8c4;'>Elberse</span> and : Sir Alex Ferguson";


console.log(str.replace(/<\w+(?=[ >])[^<>]*>|(:)/g, function(_o, O_) {

return O_ ? '' : _o;

}));

how to remove the special characters in html

In AngularJS, you need to use ng-href instead of href where there is an expression to evaluate. This ensures that it is evaluated before actually being bound to the href attribute.

In your example, <a href="'{{a.plg}}'"> would become <a ng-href="{{a.plg}}"> (note the single quotes are also removed).

Source: ng-href

How do I remove HTML special characters and replace the special character with the respective value in text?

You can't have any specific method from API to do this. Use the below method.

String text="Federation of AP Chambers of Commerce & Industry Awards for the year 2010-11. Speaking on the occasion, 
He said, "About 54 percent of the population is youth aged below 25 years. We have to use their energy and
intelligence for development of the state as well as the country.The youth trained will also be absorbed by
companies.’"";

text= replaceAll(text,""","\"");

text= replaceAll(text,"&","&");

text= replaceAll(text,"’","’");




private String replaceAll(String source, String pattern, String replacement) {
if (source == null) {
return "";
}
StringBuffer sb = new StringBuffer();
int index;
int patIndex = 0;
while ((index = source.indexOf(pattern, patIndex)) != -1) {
sb.append(source.substring(patIndex, index));
sb.append(replacement);
patIndex = index + pattern.length();
}
sb.append(source.substring(patIndex));
return sb.toString();
}

PHP, how to remove HTML special characters for a string?

The PHP function htmlspecialchars is your friend.

I've put an example from php.net below to show how it works:

<?php
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
?>

N.B. When reading php.net pages, take time to scan through the comments of any functions/ classes you might be thinking of using as they often contain real world examples of issues you may encounter.

However, I'm not entirely certain you should have asked this question, because a quick Google would have returned the results you're looking for. If you see StackOverflow or PHP.net results which seem on topic, then they're well worth a browse before posting a question which may have been asked before.

HTML Special Characters & Removing Semi Colon

You almost have it. Use html_entity_decode() instead, replace, then htmlentities() to encode again:

$str = 'Brand Name;™ SubBrand Name';
$str = html_entity_decode($str);
$str = str_replace(';', '', $str);
$str = htmlentities($str);
echo $str;

How to Remove Html Tags in PHP?

Have a look at HTML Purifier, and especially the whitelist feature.

This is probably the safest approach if you allow HTML tags. You can view the comparison here.



Related Topics



Leave a reply



Submit