我正在为我想删除HTML标签的应用程序创建RSS feed文件,此操作由strip_tags
完成。但是strip_tags
并未删除HTML特殊代码字符:
& ©
等
请告诉我可以用来从字符串中删除这些特殊代码字符的任何函数。
要么使用html_entity_decode
对其进行解码,要么使用preg_replace
将其删除:
$Content = preg_replace("/&#?[a-z0-9]+;/i","",$Content);
(来自here)
编辑:根据雅科的评论选择
最好将'+'替换为{2,8}之类的。这将限制更换整个的机会未编码的'&'为礼物。
$Content = preg_replace("/&#?[a-z0-9]{2,8};/i","",$Content);
function xmlEntities($string) {
$translationTable = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
foreach ($translationTable as $char => $entity) {
$from[] = $entity;
$to[] = '&#'.ord($char).';';
}
return str_replace($from, $to, $string);
}
我用来执行任务的功能,加入schnaader进行的升级是:
<?php
function strip_only($str, $tags, $stripContent = false) {
$content = '';
if(!is_array($tags)) {
$tags = (strpos($str, '>') !== false
? explode('>', str_replace('<', '', $tags))
: array($tags));
if(end($tags) == '') array_pop($tags);
}
foreach($tags as $tag) {
if ($stripContent)
$content = '(.+</'.$tag.'[^>]*>|)';
$str = preg_replace('#</?'.$tag.'[^>]*>'.$content.'#is', '', $str);
}
return $str;
}
$str = '<font color="red">red</font> text';
$tags = 'font';
$a = strip_only($str, $tags); // red text
$b = strip_only($str, $tags, true); // text
?>
此函数删除所有以UTF-8格式转换并准备保存在MySQL中的html标记和html符号
如果您想转换 HTML特殊字符,而不仅仅是删除它们以及剥离内容并准备纯文本,这是对我有用的解决方案...
mysql_real_escape_string(
preg_replace_callback("/&#?[a-z0-9]+;/i", function($m) {
return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES");
}, strip_tags($row['cuerpo'])))
html_entity_decode w / ENT_QUOTES | ENT_XML1会转换function htmlToPlainText($str){
$str = str_replace(' ', ' ', $str);
$str = html_entity_decode($str, ENT_QUOTES | ENT_COMPAT , 'UTF-8');
$str = html_entity_decode($str, ENT_HTML5, 'UTF-8');
$str = html_entity_decode($str);
$str = htmlspecialchars_decode($str);
$str = strip_tags($str);
return $str;
}
$string = '<p>this is ( ) a test</p>
<div>Yes this is! & does it get "processed"? </div>'
htmlToPlainText($string);
// "this is ( ) a test. Yes this is! & does it get processed?"`
之类的内容htmlspecialchars_decode转换类似'
的内容html_entity_decode转换&
之类的内容并且strip_tags删除所有剩余的HTML标签。
EDIT-添加了str_replace(',','',$ str);以及其他几个html_entity_decode()的持续测试表明它们是必需的。
您可以尝试'<
。它对我有用。
htmlspecialchars_decode($string)
使用html_entity_decode
转换HTML实体。
您需要设置字符集以使其正常工作。
除了上面的好答案之外,PHP还具有一个非常有用的内置过滤器功能:filter-var。
要删除HMTL字符,请使用:
html_entity_decode
更多信息:
$cleanString = filter_var($dirtyString, FILTER_SANITIZE_STRING);
您可能想看看htmlentities()和html_entity_decode()filter_sanitize_string
here
这可能会很好地删除特殊字符。
$orig = "I'll \"walk\" the <b>dog</b> now";
$a = htmlentities($orig);
$b = html_entity_decode($a);
echo $a; // I'll "walk" the <b>dog</b> now
echo $b; // I'll "walk" the <b>dog</b> now
一种简单的香草弦方式,而无需使用preg regex引擎:
$modifiedString = preg_replace("/[^a-zA-Z0-9_.-\s]/", "", $content);
我要做的是使用function remEntities($str) {
if(substr_count($str, '&') && substr_count($str, ';')) {
// Find amper
$amp_pos = strpos($str, '&');
//Find the ;
$semi_pos = strpos($str, ';');
// Only if the ; is after the &
if($semi_pos > $amp_pos) {
//is a HTML entity, try to remove
$tmp = substr($str, 0, $amp_pos);
$tmp = $tmp. substr($str, $semi_pos + 1, strlen($str));
$str = $tmp;
//Has another entity in it?
if(substr_count($str, '&') && substr_count($str, ';'))
$str = remEntities($tmp);
}
}
return $str;
}
,然后使用html_entity_decode
删除它们。
尝试一下
strip_tags
看起来您真正想要的是:
<?php
$str = "\x8F!!!";
// Outputs an empty string
echo htmlentities($str, ENT_QUOTES, "UTF-8");
// Outputs "!!!"
echo htmlentities($str, ENT_QUOTES | ENT_IGNORE, "UTF-8");
?>
它将命名实体替换为其等同的数字。