Php 将时间戳转换为wordpress短码
我使用这段代码从内容中检测时间戳Php 将时间戳转换为wordpress短码,php,regex,wordpress,Php,Regex,Wordpress,我使用这段代码从内容中检测时间戳 $pattern = '/(<div\s+[^<]*?\bclass="[^<"]*\btimestamp\b[^<]*?>[^<]*)?(?<!:)\d{1,2}:\d{2}(?::\d{2})?(?!(?::\d{2})?\s*[ap]\.?m\.?)(?!.*?["\'])/'; $foundTimestamp = preg_replace_callback($pattern, array($this, '
$pattern = '/(<div\s+[^<]*?\bclass="[^<"]*\btimestamp\b[^<]*?>[^<]*)?(?<!:)\d{1,2}:\d{2}(?::\d{2})?(?!(?::\d{2})?\s*[ap]\.?m\.?)(?!.*?["\'])/';
$foundTimestamp = preg_replace_callback($pattern, array($this, 'spp_ts_callback'), $content);
function spp_ts_callback($m) {
return empty($m[1]) ? '[spp-timestamp time="'. $m[0] . '"]' : $m[0];
}
但是如果我只是在它旁边放一个链接,它就不起作用了
$content='05:10 Hani shares some time-saving podcast automation tips <a href="http://wordpress.org">http://wordpress.org</a>';
$content='05:10哈尼分享了一些节省时间的播客自动化技巧';
原始正则表达式:
/(<div\s+[^<]*?\bclass="[^<"]*\btimestamp\b[^<]*?>[^<]*)?(?<!:)\d{1,2}:\d{2}(?::\d{2})?(?!(?::\d{2})?\s*[ap]\.?m\.?)(?!.*?["\'])/
/(“标记…并且您的第二个内容
“其中包含
引号…这就是为什么它没有被捕获
刚刚从正则表达式中删除了“标记:
(<div\s+[^<]*?\bclass="[^<"]*\btimestamp\b[^<]*?>[^<]*)?(?<!:)\d{1,2}:\d{2}(?::\d{2})?(?!(?::\d{2})?\s*[ap]\.?m\.?)(?!.*?[\'])
(但是你还没有输入从中获取内容的div…但是在你的正则表达式中有div???是的,我有div,它的意思是它不会检测时间戳哪个div有类“timestamp”
(<div\s+[^<]*?\bclass="[^<"]*\btimestamp\b[^<]*?>[^<]*)?(?<!:)\d{1,2}:\d{2}(?::\d{2})?(?!(?::\d{2})?\s*[ap]\.?m\.?)(?!.*?[\'])