donglu9898 2013-04-15 13:49
浏览 51
已采纳

使用preg_split避免空键值

I would like to know if there is a way to avoid empty keys in an Array while using preg_split.

The problem is that I have an input field in that every input will be split by spaces. In that case there even will be created a new Array key.

So i use:

$search_term = preg_split('/[\s]+/', $search_term);

when I will enter abc it creates an Array with:

[0] => abc

but when I will enter:

abc ...

it creates an Array like:

[0] => abc
[1] => 

it`s like it will be prepared for the value. But I would like to know if I can unset this key until it gets some value.

Thanks alot.

  • 写回答

1条回答 默认 最新

  • duanji7182 2013-04-15 13:51
    关注

    ye...

    preg_split('/[\s]+/', $search_term, -1, PREG_SPLIT_NO_EMPTY);
    

    probably best to check the documentation before asking these kind of questions though.

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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?