Preg Replace - 替换第二次出现的匹配项
我对 php 比较陌生,希望有人可以帮助我替换正则表达式,或者可能是我不太确定的匹配替换。
我想自动将(第二次出现的匹配项)加粗,然后将第四次出现的匹配项设为斜体,然后将第七次出现的匹配项加下划线。
这基本上是出于内容搜索引擎优化的目的。
我已经做了一些替换: 并认为这应该可以解决问题?
preg_replace( pattern, replacement, subject [, limit ])
我已经知道我想用的词了 假设
'pattern' is also a word that is already defined like [word].
`replacement` 'This is a variable I am getting from a mysql db.
'subject' - The subject is text from a db.
我有这个内容: 这或多或少解释了我想做的事情。
这是我要替换的文本示例。在本文中,我想让单词 example < 再次出现。大胆的。然后我想跳过文本中下次出现的示例,并使第 4 次出现的单词 example 以斜体显示。然后我想跳过文本中第五次出现 example 这个词,以及第六次,最后想要第七次 example 出现在文本中并加下划线。在此示例中,我使用超链接作为下划线示例,因为我在文本编辑器中没有看到下划线功能。 example 这个词可能在文本中出现多次,但我唯一的要求是下划线一次,粗体一次,斜体一次。我稍后可能会决定对“示例”一词进行一些引用,但这还不是优先事项。
如果该单词至少出现 7 次,则代码不要出错,这一点也很重要。
我将如何做到这一点,任何想法将不胜感激。
I am relatively new to php, and hope someone can help me with a replace regex, or maybe a match replace I am not exactly sure.
I want to automatically bold the (second occurance of a match) and then make the 4th appearance of a match italic and then the 7th appearance of a match underlined.
This is basically for SEO purposes in content.
I have done some replacements with: and were thinking this should do the trick?
preg_replace( pattern, replacement, subject [, limit ])
I already know the word I want to use in
'pattern' is also a word that is already defined like [word].
`replacement` 'This is a variable I am getting from a mysql db.
'subject' - The subject is text from a db.
Lets say I have this content: This explains more or less what I want to do.
This is an example of the text that I want to replace. In this text I want to make the second occurance of the word example < bold. Then I want to skip the next time example occurs in the text, and make the 4th time the word example appears in italic. Then I want to skip the 5th time the word example appears in the text, as well as the 6th time and lastly wants to make the 7th time example appears in the text underline it. In this example I have used a hyperlink as the underline example as I do not see an underline function in the text editor. The word example may appear more times in the text, but my only requerement is to underline once, make bold once and make italic once. I may later descide to do some quotes on the word "example" as well but it is not yet priority.
It is also important for the code not to through an error if there is not atleast 7 occurances of the word.
How would I do this, any ideas would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
preg_split
在匹配处拆分文本,应用修改,然后将所有内容重新组合在一起:第 i 个匹配的索引公式为 index = i · 2 - 1。
You could use
preg_split
to split the text at the matches, apply the modifications, and then put everything back together:The index formula for the i-th match is index = i · 2 - 1.
正则表达式本身无法计数,并且 preg_ 函数提供的帮助很少。你需要一个解决方法。如果您实际上只想搜索一个单词,您可能需要使用字符串函数。否则尝试:
技巧是有一个回调来进行记帐。在那里添加任何规则都很容易。
The regular expression itself cannot count, and the preg_ functions provide little help. You need a workaround. If you were to actually search for just a word, you might want to use string functions. Otherwise try:
The trick is to have a callback which does the accounting. And there it's quite easy to add any rules.
这是一个有趣的问题。我的实现是:
像这样使用它:
我不知道它的运行速度有多快,但它会比 preg_replace 更快。
That's an interesting question. My implementation would be:
Use it like this:
I don't know about how fast this will work, but it will be faster than preg_replace.