preg_replace 或 str_replace
我有一些文本,如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
需要什么条件?如果您有:
(即如果括号文本被交换),您是否仍要删除它?已知文本必须始终位于行首或行尾吗?如果其中任何一个的答案是肯定的,那么您最好使用正则表达式。像这样:
反斜杠 (
\
) 用于转义(
和)
字符,这些字符在正则表达式中具有特殊含义。我假设您想要更改内容 - 您可以在此时删除它们。在此正则表达式中,
.
是一个特殊运算符,意思是“任何字符”。它将匹配任何字符。+
表示“一个或更多”,并且是前一个运算符的修饰符 - 一起,这些可以被解释为匹配“一个或多个任何字符”。(
和)
字符用于捕获 子模式。通过将.+
括在括号中,我们捕获它以在替换中使用。由(
和)
包围的第一部分由正则表达式引擎标记为$1
。如果我们有更多,它们将被标记为$2
、$3
等等。如果这些都不适用,您可以使用其他答案中提到的
str_replace
函数。What are the conditions? If you have:
(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:
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.您可以简单地为 str_replace 提供要删除的字符串数组,如下所示:
如果您不需要任何巧妙的模式匹配,这将是比正则表达式更快的解决方案。
You could simply provide str_replace with an array of the strings you want to remove as such:
If you don't require any clever pattern matching, this will be a faster solution than regex.
如果这两个文本确实已知,您可以这样做:
这比正则表达式有更好的性能。第一个参数是要替换的键集,第二个参数是替换内容,第三个参数是主语。
If the two texts truly are known, you can do this:
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.