?
Solved

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

Posted on 2011-03-16
2
Medium Priority
?
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 2000 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 2000 total points
ID: 35153050
Alternatively, you could just remove anything matching \(%\w+%\) once you've substituted all the variable you have.
0

Featured Post

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
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.
Suggested Courses

762 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