douhua9726 2012-05-28 06:50
浏览 66
已采纳

PHP - 在任何字母之前删除任何数字

I am processing addresses, but I just want to remove the Street Number, eg

123 Fake St

I use the following regular expression /[^a-z ]/i' which works fine and results in

Fake St

However sometimes I have addresses such as

M4 Western Distributor Fwy

How would I keep the M4 part? Because if I run my regular expression it turns into

M Western Distributor Fwy

Any help would be appreciated, cheers

  • 写回答

3条回答 默认 最新

  • duanchun1909 2012-05-28 06:58
    关注

    Try

    /^[0-9 ]+(?=[^\d]+)/i
    

    This matches all numbers that is followed by anything other than numbers, test:

    $subject = '123 Fake St';
    var_dump(preg_replace('/^[0-9 ]+(?=[^\d]+)/i', '', $subject));
    
    $subject = 'M4 Western Distributor Fwy';
    var_dump(preg_replace('/^[0-9 ]+(?=[^\d]+)/i', '', $subject));
    

    Output:

    string(7) "Fake St"
    string(26) "M4 Western Distributor Fwy"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程