0

PHPでデータを読み取るとき、データ(文字列のチャンク)に次のようなHTML特殊文字DECIMALHEXコードが含まれているとします。
This is a sample string with < œ < and š

私が欲しいのは、文字列のチャンク内の(任意の特殊文字の)10進16進コードを検出して分割する方法ですか?

たとえば、上記の文字列には次のものが含まれます。

  • 2カウント<
  • ワンカウントœ
  • ワンカウントš

プログラムでそれを検出するにはどうすればよいですか(HTML特殊文字のOCCURRENCE)?
(収集された結果は配列としてより良くなります)

4

4 に答える 4

3

私はこれがあなたが求めているものだと思います:

$s = 'This is a sample string with œ and š';

$pattern = '/\&#x\d+\;/';

preg_match_all($pattern, $s, $matches);   

var_dump( $matches );

これは出力します:

array(1) {
  [0]=>
  array(2) {
    [0]=>
    string(7) "œ"
    [1]=>
    string(7) "š"
  }
}
于 2012-09-08T20:41:53.080 に答える
1

preg_match()を使用する必要があります-http://www.php.net/manual/en/function.preg-match.phpこのようなパターンで'/&[ 0-9a -zA-Z] {1,5}; /g'。

[更新]:必要なエンティティをメモします。それは、&#x[number][number][number];可能性のあるhtmlエンティティ(など)だけですか 、それともすべてですか?<

上記で最も一般的なケースについて説明しました。

于 2012-09-08T20:39:04.330 に答える
1

substrとstrposを使用&#して、次を検索してスキップできます;

$string = "This is a sample string with œ and š"
$hexCodes = array();
while (strlen($string) > 0) {
  if (strpos("&#") > 0) {
    $string = substr($string, strpos("&#"));
    $hex = substr($string, 0, strpos(";") + 1);
    $string = substr($string, strpos(";") + 1);
    array_push($hexCodes, $hex);
  } 
  else { break; }
}
于 2012-09-08T20:42:24.000 に答える
-2

エンティティをデコードする場合は、html_entity_decodeを使用します。次に例を示します。

<?php
$a = "I'll &quot;walk&quot; the &lt;b&gt;dog&lt;/b&gt;";

$b = html_entity_decode($a);

echo $b; // I'll "walk" the <b>dog</b> now
?>
于 2012-09-08T20:33:05.747 に答える