Solved

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

Posted on 2011-03-16
2
266 Views
Last Modified: 2012-05-11
Most of my links contain something that gets changed in the future like

(%word1%)
(%word2%)
(%domain%)
(%id%)

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?
0
Comment
Question by:Valleriani
  • 2
2 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 35153044
This works:
$txt = preg_replace("/\(%(?!(?:word1|word2|domain|id)%)\w+%\)/","",$txt);

Open in new window

0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 500 total points
ID: 35153050
Alternatively, you could just remove anything matching \(%\w+%\) once you've substituted all the variable you have.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now