duanbei3704 2012-06-15 11:40
浏览 18
已采纳

PHP preg_split最后一个char丢失在每个结果上

Following a protocol document, I can receive an array of parameters encoded as an string in which each parameter is separated from others by a blank space. The spaces within parameters are escaped with backslashes.

So, let's say we have the following strings within a parameter array:

array('Eli is beautiful', 'Fran is ugly', 'Oso is nice')

These will be encoded in just one string as follows:

Eli\ is\ beautiful Fran\ is\ ugly Oso\ is
ice

Encoding is not a major problem, but I am facing problems with decoding.

I try to split parameters using a regular expression, that should splits by spaces that are not after a backslash, so this is my code:

$params = preg_split('/[^\\\\]\s/', $str);

It splits the params as expected, but it also removes the last char for each one, so this is the output of var_dump:

array(3) {
  [0]=>
  string(15) "Eli is beautifu"
  [1]=>
  string(11) "Fran is ugl"
  [2]=>
  string(11) "Oso is nice"
}

Does someone knows how to solve this?

TIA,

  • 写回答

1条回答 默认 最新

  • dongnuo6310 2012-06-15 11:43
    关注

    Simply use negative lookbehind:

    $params = preg_split('/(?<!\\\\) /', $str);
    

    The regex above matches every space that is not preceded by a backslash, which is exactly what you intend.

    See it in action.

    Update: Your previous regex eats up letters because it matches the character preceding the space (as long as it's not a backslash); therefore that character is considered part of the delimiter and removed from the output along with the space.

    The lookbehind version asserts that no backslash precedes the space, but does not match the character -- an important difference.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址