求一正则表达式

AntiRSI 2009-09-17 12:18:44
有一字符串为如下表示:
<span style="color: rgb(102, 102, 102); position: absolute; right: 3px; top: 0pt;">1</span><span style="color: rgb(102, 102, 102); position: absolute; right: 3px; top: 0pt;">2</span><span style="color: rgb(102, 102, 102); position: absolute; right: 3px; top: 0pt;">3</span>

请问,怎么写正则表达式把红色的这三个数字提取出来,谢谢。
...全文
54 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
AntiRSI 2009-09-18
  • 打赏
  • 举报
回复
搞定,谢谢!
zqlee1216 2009-09-17
  • 打赏
  • 举报
回复
结果:
Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [1] => Array ( [0] => 102, 102, 102 [1] => 102, 102, 102 [2] => 102, 102, 102 ) [2] => Array ( [0] => 1 [1] => 2 [2] => 3 ) )
zqlee1216 2009-09-17
  • 打赏
  • 举报
回复
$str = "<span style=\"color: rgb(102, 102, 102); position: absolute; right: 3px; top: 0pt;\">1 </span> <span style=\"color: rgb(102, 102, 102); position: absolute; right: 3px; top: 0pt;\">2 </span> <span style=\"color: rgb(102, 102, 102); position: absolute; right: 3px; top: 0pt;\">3 </span>";

if(preg_match_all("|<span ([^>]+)>(.*)</[^>]+>|U",$str,$matches)){
preg_match_all("|<span style=\"color: rgb\(([0-9]{3}, [0-9]{3}, [0-9]{3})\)[^>]+>(.*)</[^>]+>|U",$str,$matches);
print_r($matches);
}
  • 打赏
  • 举报
回复
正则:
/<span[^>]+>(.*?)<\/span>/im

利用
preg_match_all(正则表达式,被查找的字符串变量,存储的结果数组变量)
那么红色部分形成的数组就是你要的结果

对结果排序使 $matches[0] 为全部模式匹配的数组,$matches[1] 为第一个括号中的子模式所匹配的字符串组成的数组,以此类推

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧