【发布时间】:2013-08-13 11:39:07
【问题描述】:
$string = "Lorem Ipsum is #simply# dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard #dummy# text ever since the 1500s, when an unknown printer took a galley of #type1# and scrambled it to #make# a #type2# specimen book. It has survived not only #five# centuries, but also the #leap# into electronic typesetting, remaining essentially unchanged";
我有一组硬编码词,例如"#simply# | #dummy# | #five# | #type1#"
我期望的输出是:
如果在
$string中找到硬代码字,它应该以黑色突出显示。喜欢"<strong>...</strong>"。如果
$string中的单词在#...#内,但在硬编码单词列表中不可用,则字符串中的这些单词应以红色突出显示。请注意,即使我们在硬编码字中包含#type1#,如果 $string 包含
#type2#或#type3#,它也应该被突出显示。
为此,我尝试如下
$pattern = "/#(\w+)#/";
$replacement = '<strong>$1</strong>';
$new_string = preg_replace($pattern, $replacement, $string);
这让我将 #..# 标记内的所有单词都突出显示。
我的怀孕很糟糕_有人可以帮忙吗?提前致谢。
【问题讨论】:
标签: preg-replace preg-match preg-match-all