dpzbzp8728 2014-10-24 08:36
浏览 36
已采纳

正则表达式的变量int php

I get some strings like phs(2.1.2):someinfo and I want to receive a version (2.1.2) in variable with hel of php and it's regexp abilities. I get it like:

        if (preg_match('/phs\((.*)\):/', $line))
        {
            echo $line.'<br>';
        }

It's ok, but now I want to get stored variable of regexp. I've found that I can get it as \1, but when I try it it's white screen in browser, so it doesn't work. Is it possible to get variables from regexp? Or there is better sollution?

  • 写回答

1条回答 默认 最新

  • duanhegn231318 2014-10-24 08:39
    关注

    Use the optional parameter of preg_match, array &$matches .
    Something like this -

    $line = "phs(2.1.2):someinfo";
    if (preg_match('/phs\((.*)\):/', $line, $matches))
    {
        $version = $matches[1];
        echo $version.'<br>';
    }
    //OUTPUT - 2.1.2
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 电脑开机过商标后就直接这样,求解各位
  • ¥15 mysql , 用自己创建的本地主机和用户名 登录不上
  • ¥15 关于#web项目#的问题,请各位专家解答!
  • ¥15 vmtools环境不正常
  • ¥15 请问如何在Ubuntu系统中安装使用microsoft R open?
  • ¥15 jupyter notebook
  • ¥30 informer时间序列预测
  • ¥20 SSR引物多态性分析
  • ¥15 大漠插件在Win11易语言注册调用和免注册灵异事件,VS上注册调用完全没问题
  • ¥15 Addressable缓存机制做热更新的问题