duanfengwang9157 2016-03-16 20:32
浏览 24
已采纳

在字母和短划线( - )php之间获取数字

I have a string that contains a letter, numbers and a dash (-). The numbers represent a specific month and a specific year.

(i.e. 'M4-16' -- April 2016)

I need to break out the 4 by itself and then the 16 by itself. I'm blanking on how to do this right now, so sorry for not including what I've tried.

Thank you in advance!

  • 写回答

1条回答 默认 最新

  • dongtu9823 2016-03-16 20:43
    关注

    Since you know the format of the relatively simple string, no regex is needed:

    $str = "M4-16";
    $parts = explode("-", $str); // Gives ["M4", "16"]
    $part1 = substr($parts[0], 1); // Gives 4
    $part2 = $parts[1]; // Gives 16
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 antv g6 力导向图布局
  • ¥15 各位为什么总是报错attributeerror呢,报nonetype object has no attribute group
  • ¥15 quartz框架,No record found for selection of Trigger with key
  • ¥15 锅炉建模+优化算法,遗传算法优化锅炉燃烧模型,ls-svm会搞,后面的智能算法不会
  • ¥20 MATLAB多目标优化问题求解
  • ¥15 windows2003服务器按你VPN教程设置后,本地win10如何连接?
  • ¥15 求一阶微分方程的幂级数
  • ¥15 关于#线性回归#的问题:【统计】回归系数要转化为相关系数才能进行Fisher' Z转化吗(相关搜索:回归模型)
  • ¥100 使用matlab解决含分段变量的优化问题
  • ¥15 matlab基于鲸鱼算法优化vmd