HTML タグを削除したいアプリケーション用の RSS フィード ファイルを作成していますstrip_tags
。ただし、strip_tags
HTML の特別なコード文字は削除されません。
& ©
等
これらの特殊なコード文字を文字列から削除するために使用できる関数を教えてください。
HTML タグを削除したいアプリケーション用の RSS フィード ファイルを作成していますstrip_tags
。ただし、strip_tags
HTML の特別なコード文字は削除されません。
& ©
等
これらの特殊なコード文字を文字列から削除するために使用できる関数を教えてください。
を使用してそれらをデコードするhtml_entity_decode
か、次を使用してそれらを削除しますpreg_replace
。
$Content = preg_replace("/&#?[a-z0-9]+;/i","",$Content);
(ここから)
EDIT:Jaccoのコメントによる代替
「+」を {2,8} などに置き換えるといいかもしれません。これにより、エンコードされていない「&」が存在する場合に、文全体を置き換える可能性が制限されます。
$Content = preg_replace("/&#?[a-z0-9]{2,8};/i","",$Content);
html_entity_decode
HTML エンティティの変換に使用します。
正しく動作させるには、charset を設定する必要があります。
上記の良い答えに加えて、PHPには非常に便利な組み込みのフィルター関数filter-varもあります。
HMTL文字を削除するには、次を使用します。
$cleanString = filter_var($dirtyString, FILTER_SANITIZE_STRING);
より詳しい情報:
ここで htmlentities() と html_entity_decode() をご覧ください。
$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
これは、特殊文字を削除するのにうまくいくかもしれません。
$modifiedString = preg_replace("/[^a-zA-Z0-9_.-\s]/", "", $content);
プレーンなバニラストリングは、preg正規表現エンジンを使用せずにそれを行う方法です。
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;
}
これを試して
<?php
$str = "\x8F!!!";
// Outputs an empty string
echo htmlentities($str, ENT_QUOTES, "UTF-8");
// Outputs "!!!"
echo htmlentities($str, ENT_QUOTES | ENT_IGNORE, "UTF-8");
?>
あなたが本当に欲しいものは次のように見えます:
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によって行われたアップグレードに参加して、タスクを実行するために使用した関数は次のとおりです。
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'])))
この関数は、MySQLに保存できるようにUTF-8で変換されたすべてのhtmlタグとhtmlシンボルを削除します
<?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
?>
$string = "äáčé";
$convert = Array(
'ä'=>'a',
'Ä'=>'A',
'á'=>'a',
'Á'=>'A',
'à'=>'a',
'À'=>'A',
'ã'=>'a',
'Ã'=>'A',
'â'=>'a',
'Â'=>'A',
'č'=>'c',
'Č'=>'C',
'ć'=>'c',
'Ć'=>'C',
'ď'=>'d',
'Ď'=>'D',
'ě'=>'e',
'Ě'=>'E',
'é'=>'e',
'É'=>'E',
'ë'=>'e',
);
$string = strtr($string , $convert );
echo $string; //aace
試すことができhtmlspecialchars_decode($string)
ます。わたしにはできる。
http://www.w3schools.com/php/func_string_htmlspecialchars_decode.asp