doumianfeng6979 2015-05-16 04:30
浏览 68
已采纳

PHP在数组中的特定字符串后获取字符串

I have a string something like aa/bb/cc/dd/55/44. Now I converted it into array using explode function.

explode("/","aa/bb/cc/dd/55/44");

Here, I want to always get the string after dd. String is dynamic i.e. dd can come in any position, but immediately after dd i want to get the string i.e. 55. How can I achieve it using PHP ?

  • 写回答

4条回答 默认 最新

  • donvo24600 2015-05-16 05:24
    关注

    However, for the record, if you wanted to safely catch all the occurrences of your pattern (not only just the last match), anything in between the dd/ and the next first /, then you may use the following:

    $re = "~(?<=dd/).*?(?=/)~"; 
    $str = "aa/bb/cc/dd/55/44/dd/66/"; 
    
    preg_match_all($re, $str, $matches);
    print_r($matches[0]);
    // output: Array ( [0] => 55 [1] => 66 )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器