【问题标题】:How to get correct list position in multi-byte string using preg_match [duplicate]如何使用 preg_match 在多字节字符串中获取正确的列表位置
【发布时间】:2012-04-14 14:28:22
【问题描述】:

我目前正在使用此代码匹配 HTML:

preg_match('/<\/?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;/u', $html, $match, PREG_OFFSET_CAPTURE, $position)

它完美匹配所有内容,但是如果我有一个多字节字符,则在返回位置时将其计为 2 个字符。

例如,返回的 $match 数组将给出如下内容:

array
  0 => 
    array
      0 => string '<br />' (length=6)
      1 => int 132
  1 => 
    array
      0 => string 'br' (length=2)
      1 => int 133

&lt;br /&gt; 匹配的实数是 128,但是有 4 个多字节字符,所以它给出 132。我真的认为添加 /u 修饰符可以让它知道发生了什么,但没有运气。

【问题讨论】:

标签: php regex multibyte multibyte-functions


【解决方案1】:

我从@Qtax 看到了这个建议:

UTF-8 characters in preg_match_all (PHP)

为了获得更多参考,这个错误在使用这个时浮出水面: Truncate text containing HTML, ignoring tags

改变的要点是这样的:

$orig_utf = 'UTF-8';
$new_utf  = 'UTF-32';

mb_regex_encoding( $new_utf );

$html     = mb_convert_encoding( $html, $new_utf, $orig_utf );
$end_char = mb_convert_encoding( $end_char, $new_utf, $orig_utf );


mb_ereg_search_init( $html );

$pattern = '</?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;';
$pattern = mb_convert_encoding( $pattern, $new_utf, $orig_utf );

while ( $printed < $limit && $tag_match = mb_ereg_search_pos( $pattern, $html ) ) {

  $tag_position = $tag_match[0]/4;
  $tag_length   = $tag_match[1];
  $tag          = mb_substr( $html, $tag_position, $tag_length/4, $new_utf );
  $tag_name     = preg_replace( '/[\s<>\/]+/', '', $tag );

  // Print text leading up to the tag.
  $str = mb_substr($html, $position, $tag_position - $position, $new_utf );

  .......

} 

同样参考截断的 HTML 页面,还有其他必要的更改:

$first_char = mb_substr( $tag, 0, 1, $new_utf );

if ( $first_char == mb_convert_encoding( '&', $new_utf ) ) {
  ...
}

我的文本编辑器是 UTF-8,所以如果我将 32 与文件的 & 符号进行比较,它将无法正常工作。

【讨论】:

    【解决方案2】:

    如果您需要快速修复并且不关心速度:

    $mb_pos = mb_strlen( substr($string, 0, $pos) );
    

    【讨论】:

      【解决方案3】:

      【讨论】:

      • 它没有我需要的相同选项:( string $pattern , string $subject [, array &amp;$matches [, int $flags = 0 [, int $offset = 0 ]]] ) vs ( string $pattern , string $string [, array $regs ] )
      猜你喜欢
      • 2013-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      • 2021-07-16
      • 2012-03-09
      相关资源
      最近更新 更多