php - 如何删除随机数量的点并获得最后一个数字?
问题描述
我正在尝试去除所有点,然后获取数字,并将 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;
}
解决方案
不确定您的输出应该是什么样子,但您可以尝试preg_replace
直接在包含所有行的变量上运行:
$lines = preg_replace("/(NAME\d+)\.+(\d+)/", "$1[$2]", $lines);
这将根据您的示例输入生成以下输出:
NAME[3]
NAME2[24]
NAME3[5]
NAME4[347]
NAME5[7]
NAME6[9]
推荐阅读
- c# - Database.open 丢失,已添加引用,仍然无法正常工作
- c# - 使用 EF Core 编写用于排序和投影的动态 LINQ 查询
- javascript - 如何获取带有 chrome 扩展名的对象的 url
- maven - Gradle - 下载依赖项时间歇性地获取套接字关闭异常
- html - flex-wrap 没有将元素包装到下一行
- postgresql - 使用 st_distance postgresql 寻找两个位置(lat,lng)的距离
- python - 为什么我在使用 boto ecr 客户端登录后拒绝推送 docker?
- c# - 无法对视图中的空引用执行运行时绑定
- javascript - React Native - 从 Child 调用 Parent ref 函数
- ansible - ansible“取消归档”模块未按预期工作