replace a string unless encapsulated

If I have a string like this
$string = "{fantastic|great|brilliant}! This is great";
If I would like to replace the bolded great only in this, and not anything that occurs between the { and } characters.
how would I go about that?

Who is Participating?
Carl BohmanConnect With a Mentor Commented:
String: great{fantastic|great|brilliant}! This is great{fantastic|great|brilliant}greatgreatgre atgreat
Regex: /(?:^|\G)([^{]*?(?:\{[^}]*\}[^{]*?)*?)(great)/
Replacement: \1FOO

I ran my regex through an online regex tester ( and it seemed to work as described.  Here's some (untested) source code that should do what you are looking for:

$sourcestring="great{fantastic|great|brilliant}! This is great{fantastic|great|brilliant}greatgreatgre atgreat";
echo preg_replace('/(?:^|\G)([^{]*?(?:\{[^}]*\}[^{]*?)*?)(' . $searchstring . ')/', '\1' . $replacestring, $sourcestring);

Open in new window

Greg AlexanderLead DeveloperCommented:
Like this:

$string = "{fantastic|great|brilliant}! This is great";
$new_string = explode(" ",$string);
$new_string = implode(" ",$new_string);
echo $new_string;

Open in new window

käµfm³d 👽Commented:
If you can safely assume that the words adjoining "great" inside the braces will be there, and be in that order, then you can do something like the following. If you instead will not know the words inside the braces, then it might be a bit more difficult and you might have to look into something like explode().
$string = "{fantastic|great|brilliant}! This is great";

$result = preg_replace('/(?<!{fantastic\\|)great(?!\\|brilliant})/', "replacement", $string);

echo $result;

Open in new window

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

paulp75Author Commented:
Thanks for the replies, but none will get the result that I require.
what actually happens is it runs through a loop of cases. I can't assume where the words will be etc. They might have white space or might not (it comes from another variable)

so this might actually be a article that is 500 words or so.

but it will need to find cases of each one, and replace it.
so the words would vary each time.

In the example, the word great would likely be getting replaced by a line of text in a text file.

$newtext = str_replace($phrase, "{" . $line . "}", $newtext);

but how would I replace only the ones not inside the {}'s
this {is not|will not|should not} to be replaced, however this should not be missed
In the above example, on the instance that is not inside the { and } should be replaced.
This will be replaced by a similar text with { and } afterward and multiple terms.
käµfm³d 👽Commented:
How about this? You can change the "temp" replacement (##########) to whatever you like; just make sure it's not a string that already exists in your source string.
$string = "{fantastic|great|brilliant}! This is great{fantastic|great|brilliant}";

preg_match_all('/{[^}]*}/', $string, $matches);

$result = preg_replace('/{[^}]*}/', '##########', $string);

$result = str_replace('great', 'replacement', $result);

for ($i = 0; $i < count($matches); $i++)
    $count = 1;

    $result = str_replace('##########', $matches[$i][0], $result, $count);

echo $result;

Open in new window

Ray PaseurCommented:
Thanks for the replies, but none will get the result that I require.

That's because the problem definition in the original question is not semantically clear.  Here is what might get you a better answer...

Create a small set of test cases that demonstrate the variations on the theme.  For each test case, manually create the exact result output you would expect to get after processing.  Post those test and results sets here and let the question be, "What is a generalized, best-practices, method of handling this?"  This is one of the steps in test-driven design, and it makes for very easy and accurate program development.
käµfm³d 👽Commented:

Nice  : )
paulp75Author Commented:
Fantastic solution. thank you very much.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.