Solved

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

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

724 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