سؤال

???? ??? ??????? ???? ?? ??? ??????:

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

?? ????? ?? ?????? ??? ??? ?? ??? ????? ? ??? ?????? ?? ( TEXT ??????? ?? ???) ? (??? ???? ????? ???) .

???? ??? ????? ?? preg_replace ?????? ?? ????? ????? ?? ???? ??????? ?????? ???????? ???:

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

??? ??? ..

Unknown Text

???? ??? preg_replace ?????? 3 ????????? ???? ???????? ???????? ???? ???? ?????? ? ??????? .

?? ???? ????? ?????? ???? ???? ??? ????? ??? ??????? ???? ??????? OR? ????? ?reaplce ?????? ??????? ??? ??? (TEXT ??????? ?? ???) ? (??? ????? ?????? KNOW TEXT) ??? ????? ???? ??? ?????? ???? ?? ???? ???? ??? str_replace () ?? ?????

??????

??? --- --- UPDATE

??? ???? (??? ????? ?????? TEXT ??????) ???? ?? ????? ???? ?? ????? ???? ?? ???? ?????? ?? ?? ???? ??? ??? (TEXT ??????? ?? ???) ?? ??? ????? (??? ???? ????? ???) . ???? ?? ???? ?????????? 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.

نصائح أخرى

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.

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top