Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP来检测和拆分字符串中的html特殊字符代码?_Php_Unicode_Utf 8_Hex_Special Characters - Fatal编程技术网

PHP来检测和拆分字符串中的html特殊字符代码?

PHP来检测和拆分字符串中的html特殊字符代码?,php,unicode,utf-8,hex,special-characters,Php,Unicode,Utf 8,Hex,Special Characters,在PHP中,当我读取数据时,假设数据(字符串块)包含HTML特殊字符十进制十六进制代码,如: 这是一个带有œ;和š 我想知道的是,如何检测并拆分字符串块中的十进制十六进制代码(任何特殊字符) 例如,上面的字符串包含: 两次计数 一次计数和#x153 一次计数和#x161 如何通过编程检测它(任何Html特殊字符的出现)? (收集的结果作为一个数组会更好)如果您想解码实体,请使用html\u entity\u decode。 以下是一个例子: <?php $a = "

在PHP中,当我读取数据时,假设数据(字符串块)包含HTML特殊字符十进制十六进制代码,如:
这是一个带有œ;和š

我想知道的是,如何检测并拆分字符串块中的十进制十六进制代码(任何特殊字符)

例如,上面的字符串包含:

  • 两次计数
  • 一次计数
    和#x153
  • 一次计数
    和#x161
如何通过编程检测它(任何Html特殊字符的出现)?

(收集的结果作为一个数组会更好)

如果您想解码实体,请使用html\u entity\u 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
?>

如果要解码实体,请使用html\u entity\u 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
?>
您应该使用preg_match()-和这样的模式“/&[0-9a-zA-Z]{1,5}/g'

[更新]:注意您需要的实体。那只是
&#x[number][number][number]
或所有可能的html实体(如
e.t.c.)

上面我描述了最常见的情况。

您应该使用preg_match()-和这样的模式“/&[0-9a-zA-Z]{1,5}/g'

[更新]:注意您需要的实体。那只是
&#x[number][number][number]
或所有可能的html实体(如
e.t.c.)


上面我描述了最常见的情况。

我想这就是你想要的:

$s = 'This is a sample string with &#x153; and &#x161;';

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

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

var_dump( $matches );
这将输出:

array(1) {
  [0]=>
  array(2) {
    [0]=>
    string(7) "&#x153;"
    [1]=>
    string(7) "&#x161;"
  }
}

我想这就是你想要的:

$s = 'This is a sample string with &#x153; and &#x161;';

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

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

var_dump( $matches );
这将输出:

array(1) {
  [0]=>
  array(2) {
    [0]=>
    string(7) "&#x153;"
    [1]=>
    string(7) "&#x161;"
  }
}

您可以使用substr和strpos查找
&
并跳到下一个

$string = "This is a sample string with &#x153; and &#x161;"
$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; }
}

您可以使用substr和strpos查找
&
并跳到下一个

$string = "This is a sample string with &#x153; and &#x161;"
$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; }
}


不,那么您如何知道/检测您得到了2x
和2x
和2x
在字符串中有??此函数对HTML实体的每次出现进行解码。你不需要知道它出现了多少次。哼???你怎么说我不需要知道????那么,当你需要知道计数时,你将如何计数????奇怪的回答@4lvin我在帖子中提到,如果你只想解码,那么就用这个。如果您想计算发生的次数,我认为您需要创建一个包含所有html实体的数组,并在给定文本中搜索每个html实体。这将是一个耗时的过程,但我现在想不出任何其他方法。不,那么您如何知道/检测您得到了2x
和2x
和2x
在字符串中有??此函数对HTML实体的每次出现进行解码。你不需要知道它出现了多少次。哼???你怎么说我不需要知道????那么,当你需要知道计数时,你将如何计数????奇怪的回答@4lvin我在帖子中提到,如果你只想解码,那么就用这个。如果您想计算发生的次数,我认为您需要创建一个包含所有html实体的数组,并在给定文本中搜索每个html实体。这将是一个耗时的过程,但我现在想不出任何其他方法。那么呢?嗨@Gumbo,你在这里找到关于复制的任何线索吗?好吧,在你第四次编辑之后,就不多了。那么呢?嗨@Gumbo,你在这里找到关于复制的任何线索吗?嗯,在第四次编辑之后,就不会有太多了。preg_match_all可能更优雅了。这是“艰难”的道路;-)preg_match_all可能更优雅。这是“艰难”的道路;-)实际上,对于所有可能的html实体。抱歉,我通过添加更多基本html字符模式修改了我的问题。实际上,对于所有可能的html实体。抱歉,我通过添加更多基本html字符模式修改了我的问题。抱歉,我通过添加更多基本html字符模式修改了我的问题。您可以为此更改模式。但是我认为你想要替换,所以看看preg_replace。我认为我们应该正确地表述你的问题,不更改是4个计时器。对不起,我修改了我的问题,添加了更多基本的html字符模式。你可以为此更改模式。但是我认为你想要替换,所以看看preg_replace。我认为我们应该正确地表述你的问题,不改变是4个计时器。