我如何删除随机数量的点并得到最后的数字?



我试图去掉所有的点,然后得到数字,并将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('/rn|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;
}

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

$lines = preg_replace("/(NAMEd+).+(d+)/", "$1[$2]", $lines);
这将根据您的示例输入生成以下输出:
NAME[3]
NAME2[24]
NAME3[5]
NAME4[347]
NAME5[7]
NAME6[9]

可以使用以下函数:

function introExcerpt($str, $id = null, $introExcerptCut = null)
{
$fileInfo['intro'] = $str;
$introExcerpt = trim($fileInfo['intro']);
$lines = preg_split('/rn|r|n/', $introExcerpt);
$intro = '<div class="toc"><ul class="toc">';
for ($i = 0; $i < count($lines); $i++) {
$intro .= '<li>';
$tmpLineArray = explode('.', $lines[$i]);
array_filter($tmpLineArray, function ($value) {
return !is_null($value) && $value != '';
});
foreach ($tmpLineArray as $value) {
$intro .= $value . ' ';
}

$intro .= '</li>';
}
$intro .= '</ul></div>';
return $intro;
}

使用点作为分隔符将整行分割成数组,并过滤掉空元素。

最新更新