views:

371

answers:

3

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.

+2  A: 

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.

Matchu
+3  A: 

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.

middaparka
+3  A: 

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.

Samir Talwar
It works! Can you explain how $1 is populated with the item I want to keep?
OneNerd
It works, but has lower performance, so make sure that you do *not* want the result if the two undesired items are elsewhere in the string.
Matchu
@OneNerd: I've added the information to the answer, along with links for more information on the `.` and `+` operators.
Samir Talwar