Solved

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

Posted on 2011-03-16
2
277 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 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