douxiong2738 2011-08-26 23:38
浏览 77
已采纳

使用preg_match_all和度数符号时遇到问题

I am having trouble with a preg_match_all on a string that contains a degree symbol. The sample of code is below.

//Sample data
$x = "<array_0>
        <id>text-21650</id>
        <text>Lat/Long 38° 57' 34 N,  106° 21' 38 W</text>
      </array_0>";

$reels = '/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*)<\/\s*\\1\s*>)/s';

preg_match_all($reels, $x, $elements);

foreach ($elements[1] as $ie => $xx) {
  $name = $elements[1][$ie];
  $cdend = strpos($elements[3][$ie], "<");
  if ($cdend > 0) {
    $xmlary[$name] = substr($elements[3][$ie], 0, $cdend - 1);
  }

  if (preg_match($reels, $elements[3][$ie]))
    $xmlary[$name] = processEl($elements[3][$ie]);
  else if ($elements[3][$ie] !== null) {
    $xmlary[$name] = $elements[3][$ie];
  }
}

For some reason it doesn't work properly with the degree symbols in there. If I take it out it works. I would really like to find a way that they can stay in there without changing them. I am also wondering if there may be other extended character that could cause a problem too.

Any help would be greatly appreciated. Thanks

  • 写回答

2条回答 默认 最新

  • dsp1836 2011-08-26 23:44
    关注

    Have a look at this previous answer on StackOverflow.

    Basically you will have to switch to Unicode matching.

    Use mb_ereg_match instead to support UTF-8 chars. Docs: http://php.net/manual/en/book.mbstring.php

    Initialize mb* like this:

    mb_regex_encoding('UTF-8'); mb_internal_encoding('UTF-8');

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法