在 PHP 中使用正则表达式有选择地替换标签之外的单词?
我有一段文本,我想使用 PHP (preg_replace) 替换一些单词。以下是一段示例文本:
本课程介绍押韵的[一个]单词以及学生可以学习识别这些单词的方法[/一个]。主题包括学习押韵单词听起来相似,并且这些声音都来自单词的[two]结尾[/two]。这可以通过让学生重复一组押韵的单词来实现,这样他们就能说出来并听到它们。学生们还将参加各种押韵单词活动。为了展示掌握程度,学生将听[三]组单词[/三]并告诉老师这些单词是否押韵。
如果您注意到“单词”一词出现了很多次。我想用“birds”一词替换任何标签中不出现的所有情况。所以它看起来像这样:
本课程介绍学生可以学习识别这些鸟类的押韵[一个]单词和方法[/一]。主题包括学习押韵的鸟听起来很相似,并且这些声音都来自单词的[two]结尾[/two]。这可以通过让学生重复一组押韵的鸟来实现,这样他们就可以说出来并听到它们。学生们还将参加各种押韵单词活动。为了展示掌握程度,学生将听[三]组单词[/三]并告诉老师鸟儿是否押韵。
您会使用正则表达式来完成此任务吗?
正则表达式可以完成这个任务吗?
I have a paragraph of text and i want to replace some words using PHP (preg_replace). Here's a sample piece of text:
This lesson addresses rhyming [one]words and ways[/one] that students may learn to identify these words. Topics include learning that rhyming words sound alike and these sounds all come from the [two]ending of the words[/two]. This can be accomplished by having students repeat sets of rhyming words so that they can say them and hear them. The students will also participate in a variety of rhyming word activities. In order to demonstrate mastery the students will listen to [three]sets of words[/three] and tell the teacher if the words rhyme or not.
If you notice there are many occurances of the word 'words'. I want to replace all the occurances that don't occur inside any of the tags with the word 'birds'. So it looks like this:
This lesson addresses rhyming [one]words and ways[/one] that students may learn to identify these birds. Topics include learning that rhyming birds sound alike and these sounds all come from the [two]ending of the words[/two]. This can be accomplished by having students repeat sets of rhyming birds so that they can say them and hear them. The students will also participate in a variety of rhyming word activities. In order to demonstrate mastery the students will listen to [three]sets of words[/three] and tell the teacher if the birds rhyme or not.
Would you use regular expressions to accomplish this?
Can a regular expression accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您拥有的标签没有定义正则语言,因此正则表达式对此不起作用。我认为你能做的最好的事情就是删除所有标签(用其他东西替换它们),用“鸟”替换“单词”,然后将内容放回去。
The tags you have don't define a regular language and thus regular expressions won't work well for this. The best I think you can do is remove all the tags (replace them with something else), replace "words" with "birds" and then put the content back.