PHP w/ Regex? Removing anything not containing the following.

Most of my links contain something that gets changed in the future like


However, I don't want INCORRECT variables to be added, when they hit save I want them to delete unless its one of the ABOVE. the vars are always wrapped in (% and %). So something like this:

Hello (%word1%), This is a test (%shoulddelete%) (%word2%)
would become
Hello (%word1%), This is a test  (%word2%)

Any advice?
Who is Participating?
Terry WoodsConnect With a Mentor IT GuruCommented:
This works:
$txt = preg_replace("/\(%(?!(?:word1|word2|domain|id)%)\w+%\)/","",$txt);

Open in new window

Terry WoodsConnect With a Mentor IT GuruCommented:
Alternatively, you could just remove anything matching \(%\w+%\) once you've substituted all the variable you have.
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.