在 PHP 中使用正则表达式有选择地替换标签之外的单词?

发布于 2024-08-27 03:46:52 字数 571 浏览 5 评论 0原文

我有一段文本,我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剩余の解释 2024-09-03 03:46:52

您拥有的标签没有定义正则语言,因此正则表达式对此不起作用。我认为你能做的最好的事情就是删除所有标签(用其他东西替换它们),用“鸟”替换“单词”,然后将内容放回去。

$str = preg_replace_callback('!\[one\].*?\[/one\]!s', 'hash_one', $input);
$str = str_replace('words', 'birds', $str);
$output = preg_replace_callback('!:=%\w+%=:!', 'replace_one', $str);

$hash = array();

function hash_one($matches) {
  global $hash;
  $key = ':=%' . md5($matches[0]) . '%=:'; // to ensure this doesn't occur naturally
  $hash[$key] = $matches[0];
  return $key;
}

function replace_one($amtches) {
  global $hash;
  $ret = $hash[$matches[0]];
  return $ret ? $ret : $matches[0];
}

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.

$str = preg_replace_callback('!\[one\].*?\[/one\]!s', 'hash_one', $input);
$str = str_replace('words', 'birds', $str);
$output = preg_replace_callback('!:=%\w+%=:!', 'replace_one', $str);

$hash = array();

function hash_one($matches) {
  global $hash;
  $key = ':=%' . md5($matches[0]) . '%=:'; // to ensure this doesn't occur naturally
  $hash[$key] = $matches[0];
  return $key;
}

function replace_one($amtches) {
  global $hash;
  $ret = $hash[$matches[0]];
  return $ret ? $ret : $matches[0];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文