You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP CodeIgniter中搜索并截取数据库字段中的特定编号?

解决方案

提取注册编号

要从通知内容中精准提取指定格式的注册编号,正则表达式是比explode更合适的方案,它能精准匹配带格式的目标内容。示例代码如下:

// 从数据库获取通知内容(CodeIgniter 写法)
$notification = $this->db->select('notification_content')
                         ->where('id', $your_id) // 替换为实际记录ID
                         ->get('your_table_name') // 替换为实际表名
                         ->row()->notification_content;

// 匹配所有符合规则的注册编号
preg_match_all('/reg no\.?\s*<strong>([A-Z0-9]+)<\/strong>/i', $notification, $matches);
$reg_numbers = $matches[1];

// 输出提取到的编号
foreach ($reg_numbers as $num) {
    echo "<p>$num</p>";
}

为编号添加链接

如果需要直接将原通知中的编号替换为可点击的链接,使用preg_replace实现:

// 定义替换规则,将编号替换为带链接的格式
$pattern = '/reg no\.?\s*<strong>([A-Z0-9]+)<\/strong>/i';
// 替换为实际跳转链接,$1会自动替换为匹配到的编号
$replacement = 'reg no. <a href="' . base_url('registration/detail/$1') . '"><strong>$1</strong></a>';

$updated_notification = preg_replace($pattern, $replacement, $notification);

// 输出处理后的通知内容
echo $updated_notification;

为什么explode无法满足需求

explode('no.', $notification)只能按固定字符串分割内容,得到的结果包含大量冗余HTML标签和文本,无法直接提取目标编号;而正则表达式可以精准定位到reg no./reg no之后的<strong>标签内的编号内容。

内容的提问来源于stack exchange,提问作者Anaswara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 17:15:10