关于匹配本文中几小时几分钟的有关问题

WBOY
Release: 2016-06-13 11:58:21
Original
980 people have browsed it

关于匹配本文中几小时几分钟的问题
一段文本可能会出现这样几种情况:
用了1小时43分钟
用了1小时
用了43分钟

要实现的是从文本中匹配出这个时间,即加粗部分
现在想到的正则表达式是preg_match('/([0-9]+小时[0-9]+分钟|[0-9]+小时|[0-9]+分钟)/is', $txt, $matches)
虽然能够正确匹配出想要的结果,但是感觉不够简洁,请问如何改进?
------解决方案--------------------
加粗只是视觉效果,分开来加并无影响

$s =<<< TXT<br />一段文本可能会出现这样几种情况:<br />用了1小时43分钟<br />用了1小时<br />用了43分钟<br />TXT;<br /><br />echo preg_replace('/\d+(小时<br><font color='#FF8000'>------解决方案--------------------</font><br>分钟)/', '<b>$0</b>', $s);<br />
Copy after login
一段文本可能会出现这样几种情况:<br />用了<b>1小时</b><b>43分钟</b><br />用了<b>1小时</b><br />用了<b>43分钟</b><br />
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!