如何使用 preg_replace(_callback) 依次替换主题标签文本
目标文本类似于:
Lorem ipsum dolor #sit# amet, consectetur #adipisicing# elit, sed do #eiusmod# tempor incididunt ut Labore et dolore magna aliqua。 Ut enim ad minim veniam, quis nostrud exeritation ullamco Laboris nisi ut aliquip.
标签 #sit#、#adipisicing#、#eiusmod# 的核心对应数据将类似于数组:
array(
'42' => 'sit somewhere',
'43' => 'adipisicing...',
'63' => 'eiusmod...'
)
我想使用 preg_replace 或 preg_replace_callback 来按顺序替换数组值标记的标签。得到的结果是:
Lorem ipsum dolor <a href="/link/to/42" title="sit somewhere">#sit#</a> amet, consectetur <a href="link/to/43" title="adipisicing...">#adipisicing#</a> elit, sed do <a href="/link/to/63" title="eiusmod...">#eiusmod#</a> tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip.
如何实现?
the target text is something like:
Lorem ipsum dolor #sit# amet, consectetur #adipisicing# elit, sed do #eiusmod# tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip.
the coresponding data to hashtagged #sit#, #adipisicing#, #eiusmod# would be array like:
array(
'42' => 'sit somewhere',
'43' => 'adipisicing...',
'63' => 'eiusmod...'
)
i want to use preg_replace or preg_replace_callback to replace the hashtagged by the array value in sequence. to get result as:
Lorem ipsum dolor <a href="/link/to/42" title="sit somewhere">#sit#</a> amet, consectetur <a href="link/to/43" title="adipisicing...">#adipisicing#</a> elit, sed do <a href="/link/to/63" title="eiusmod...">#eiusmod#</a> tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip.
how could that be achieved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 str_replace 可能会更好。
请参阅 http://www.php.net/manual/en/function。 str-replace.php
You are probably better off using str_replace.
See http://www.php.net/manual/en/function.str-replace.php