preg_replace 或 str_replace

发布于 2024-08-16 16:51:07 字数 869 浏览 6 评论 0原文

我有一些文本,如下所示:

Paragraphs of text
(SOME KNOWN TEXT)Unknown Text(SOME OTHER KNOWN TEXT)
Some additional paragraphs of text

我想要的是保留未知文本,但删除(某些已知文本)(其他一些文本)已知文本)

我认为 preg_replace 会给我我想要的,但我需要正则表达式来替换它:

(SOME KNOWN TEXT)Unknown Text(SOME OTHER KNOWN TEXT)

用这个..

Unknown Text

我看到 preg_replace 需要 3 个参数,regex替换文本主题

有没有办法将未知文本的值传递给替换文本参数,或者,有办法重新获取左右部分(一些已知文本)(一些其他已知文本) 不删除未知文本?或者,是否有一个 str_replace() 解决方案?

谢谢

---更新---

(一些其他已知的文本)片段可能存在于文本中的其他位置,但我不希望将其删除,除非它遵循(的模式一些已知文本)未知文本(一些其他已知文本)。我认为最初的 str_replace 建议因此不起作用。

I have some text, something like this:

Paragraphs of text
(SOME KNOWN TEXT)Unknown Text(SOME OTHER KNOWN TEXT)
Some additional paragraphs of text

What I want is to keep the Unknown Text, but get rid of the (SOME KNOWN TEXT) and (SOME OTHER KNOWN TEXT).

I think the preg_replace will give me what I want, but I need the regular expression to replace this:

(SOME KNOWN TEXT)Unknown Text(SOME OTHER KNOWN TEXT)

with this ..

Unknown Text

I see the preg_replace takes 3 parameters, the regex, the replacement text, and subject.

Is there a way to pass the value of unknown text to the replacement text parameter, OR, a way to reaplce the left and right pieces (SOME KNOWN TEXT) and (SOME OTHER KNOW TEXT) without removing the Unknown Text? Alternatively, is there a str_replace() solution to this?

Thanks

---UPDATE---

The (SOME OTHER KNOWN TEXT) piece may exist in other places in the text, but I don't want it removed unless it follows the pattern of (SOME KNOWN TEXT)Unknown Text(SOME OTHER KNOWN TEXT). I am thinking the initial str_replace suggestions would not work because of this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

寄离 2024-08-23 16:51:07

需要什么条件?如果您有:

(一些其他已知文本)未知文本(一些已知文本)

(即如果括号文本被交换),您是否仍要删除它?已知文本必须始终位于行首或行尾吗?如果其中任何一个的答案是肯定的,那么您最好使用正则表达式。像这样:

$new_text = preg_replace('/^\(SOME KNOWN TEXT\)(.+)\(SOME OTHER KNOWN TEXT\)$/m, '$1', $text);

反斜杠 (\) 用于转义 () 字符,这些字符在正则表达式中具有特殊含义。我假设您想要更改内容 - 您可以在此时删除它们。

在此正则表达式中,. 是一个特殊运算符,意思是“任何字符”。它将匹配任何字符。 + 表示“一个或更多”,并且是前一个运算符的修饰符 - 一起,这些可以被解释为匹配“一个或多个任何字符”。 () 字符用于捕获 子模式。通过将 .+ 括在括号中,我们捕获它以在替换中使用。由 () 包围的第一部分由正则表达式引擎标记为 $1。如果我们有更多,它们将被标记为 $2$3 等等。

如果这些都不适用,您可以使用其他答案中提到的 str_replace 函数。

What are the conditions? If you have:

(SOME OTHER KNOWN TEXT)Unknown text(SOME KNOWN TEXT)

(i.e. if the bracketing text is swapped), do you still want to remove it? Must the known text always be at the beginning or end of the line? If the answer is yes to either of these, you're probably better off using a regex. Something like this:

$new_text = preg_replace('/^\(SOME KNOWN TEXT\)(.+)\(SOME OTHER KNOWN TEXT\)$/m, '$1', $text);

The backslashes (\) are to escape the ( and ) characters, which have a special meaning in regular expressions. I assume that you'll want to change the content - you can remove them at that point.

In this regex, the . is a special operator meaning "any character." It will match any character. The + means "one or more", and is a modifier on the previous operator - together, these can be interpreted as matching "one or more of any character". The ( and ) characters are used for capturing sections of the text in a subpattern. By encompassing the .+ in parentheses, we capture this to be used in the replacement. The first section surrounded by ( and ) are labelled $1 by the regex engine. If we had more, they'd be labelled $2, $3, and so on.

If neither of these apply, you can use the str_replace function as mentioned in the other answers.

晨曦÷微暖 2024-08-23 16:51:07

您可以简单地为 str_replace 提供要删除的字符串数组,如下所示:

$destString = str_replace(array('TEXT A', 'TEXT B'), '', $sourceString);

如果您不需要任何巧妙的模式匹配,这将是比正则表达式更快的解决方案。

You could simply provide str_replace with an array of the strings you want to remove as such:

$destString = str_replace(array('TEXT A', 'TEXT B'), '', $sourceString);

If you don't require any clever pattern matching, this will be a faster solution than regex.

梦旅人picnic 2024-08-23 16:51:07

如果这两个文本确实已知,您可以这样做:

$result = str_replace(array('(SOME KNOWN TEXT)', '(SOME OTHER KNOWN TEXT)'), '', '(SOME KNOWN TEXT)Unknown Text(SOME OTHER KNOWN TEXT)');

这比正则表达式有更好的性能。第一个参数是要替换的键集,第二个参数是替换内容,第三个参数是主语。

If the two texts truly are known, you can do this:

$result = str_replace(array('(SOME KNOWN TEXT)', '(SOME OTHER KNOWN TEXT)'), '', '(SOME KNOWN TEXT)Unknown Text(SOME OTHER KNOWN TEXT)');

which is better performance than a regular expression. The first argument is the set of keys to replace, the second argument is what to replace it with, and the third is the subject.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文