首页 > 解决方案 > 如何删除随机数量的点并获得最后一个数字?

问题描述

我正在尝试去除所有点,然后获取数字,并将 NAME[X] 作为输出。

我的输出是:

NAME..................................................................................................3
NAME2...................................................................................................24
NAME3...............................................................................................................................................5
NAME4.......................347
NAME5............................................................................................7
NAME6......................................................................9

到目前为止,我已经尝试过这样的事情:

function introExcerpt($id = null, $introExcerptCut = null)
    {
     
        $fileInfo['intro'] = 'my string';
        $introExcerpt = trim($fileInfo['intro']);

        $lines = preg_split('/\r\n|\r|\n/', $introExcerpt);
        $intro = '<div class="toc"><ul class="toc">';
        
        for ($i = 0; $i < count($lines); $i++) {
           // if (isset($lines[$i]) && substr(trim($lines[$i]), -1) !== '.') {
                $intro.= $lines[$i].'<br />';
            //}
        }
        $intro .= '</div></ul>';

        return $intro;
    }
    

标签: php

解决方案


不确定您的输出应该是什么样子,但您可以尝试preg_replace直接在包含所有行的变量上运行:

$lines = preg_replace("/(NAME\d+)\.+(\d+)/", "$1[$2]", $lines);

这将根据您的示例输入生成以下输出:

NAME[3]
NAME2[24]
NAME3[5]
NAME4[347]
NAME5[7]
NAME6[9]

推荐阅读