preg_replace 之间 >且<
我有这个:
<div> 16</div>
并且我想要这个:
<div><span>16</span></div>
目前,这是我可以使其工作的唯一方法:
preg_replace("/(\D)(16)(\D)/", "$1$2 $3", "
16")
如果我省略 $3,我会得到:
<div><span>16</span>/div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不太确定您的需求是什么,但以下内容更通用:
这会导致:
即使该值是:
它会导致:
Not quite sure what your after, but the following is more generic:
Which would result in:
Even if the value were:
It would result in:
我认为您的意思是说您正在使用以下内容:
这没有任何问题。 $3 将包含第二个 (\D+) 组中匹配的所有内容。如果你不去管它,显然它不会神奇地出现。
请注意,问题中的代码有一些错误:
\
。\D+
来匹配多个字符。\s*
来匹配任意数量的空白字符。I think you meant to say you're using the following:
There's nothing wrong with that. $3 is going to contain everything matched in the second (\D+) group. If you leave it off, obviously it's not going to magically appear.
Note that your code in the question had some errors:
\
's in a string.\D+
to match multiple characters.\s*
to match any number of whitespace characters.尝试以下操作 -
Try following -
这就是最终效果最好的:
我发现我不确定页码之前或之后有哪些标签。
This is what ended up working the best:
What I found was that I didn't know for sure what tags would precede or follow the page number.