This code creates a user managed regex generator. You can use it to apply regexs to any content string. The expression and replacement arrays are admined by a form that allows the user to enter array items seperately.
By : drastik
/*This is the Regex Generator Code. This code allows you to use a form in a content management situation to create and impliment regualr expressions. Contact: tysontune@comcast.net
The first file you need to create is regex.php. This file will actually do all the work. Note that I am using the variable $content to hold my strings for regexing. You can either name the variable holding your text $content, or you can change the name in the script to whatever you like to use. Just keep in mind that this variable must be consistent.
Also, you will notice two files called expression.txt and replace.txt. These files should be blank .txt files stored wherever you like, but you probably want them secure or someone could turn all your the's into poop's*/
<?
//read the files into arrays
$expression = file('path/to/expression.txt');
$replace = file('path/to/replace.txt');
//replace via array
$content = preg_replace($expression,$replace,$content);
?>
/*Now you just call regex.php with an include function. You can do this where ever you like as long as your paths are correct.*/
<?
$content = "string" //this is you un regexed text
include('path/to/regex.php'); //this pulls in regex.php and runs $content through it
echo $content; //display the newly regexed text, of course you can do whatever with it
?>
/*Now you need to build the form for entering you expressions and replacements. We'll call it regexform.php, but you could really put the code anywhere, persaonly I have it in the admin pages of the blogging app I'm building. This page takes info from a form and saves it into those previously balnk .txt files*/
<?
//regexform.php
//if information is present process the form
if ($_POST['expression'] && $_POST['replace'])
{
$replace = stripslashes($_POST['replace'])."\n";
$expression = stripslashes($_POST['expression']);
$expression = "/".$expression."/\n";
$exp = "path/to/expression.txt";
$fh = fopen($exp,"a");
fwrite($fh,$expression);
fclose($fh);
$rep = "path/to//replace.txt";
$fh2 = fopen($rep,"a");
fwrite($fh2,$replace);
fclose($fh2);
}
//print the form
print "<form action=\"regexform.php\" method=\"post\">";
print "expression:<br /><input type=\"text\" name=\"expression\" size=\"30\" /><br />";
print "replacement:<br /><input type=\"text\" name=\"replace\" size=\"30\" /><br />";
print "<input type=\"submit\" name=\"submit\" value=\"enter regex\" />";
print "</form>";
?>
/*Now you can enter your expression and replacement into the form above and they are automatically added to the regex arrays. The order they come in is important to the array structure, so don't delete from one list without deleting the matching item from the other list.
If you have tried to use this code and are having a problem, the usual culprits are paths. I wrote this to be used on multiple pages in a site from a remote location, and anytime you use include() you need to check not only the paths in your main page, but also any page you are includ()ing.*/
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Content Management Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
This demonstration gives you an overview of IBM® Rational® Build Forge Express Edition, a global offering that provides a framework to automate and execute software processes. Rational Build Forge provides a software assembly line that can support all of your tools, technologies, and platforms so you can achieve a repeatable, reliable, and traceable build and release process. FREE! Go There Now!
|
|
|
|
Hear how IBM Rational Project and Portfolio Management integrated solutions help teams put the right tools and processes in place to maximize the effectiveness and efficiency of project teams and ensure that the business vision is being executed correctly. Learn how to automate and integrate requirements prioritization, top-down project planning, communications and controls, and methodology deployment to keep your scope, costs, and schedules under control. Tackle with an end-to-end approach the management of scope and scope changes, usage of methodology to control and empower project teams, and optimization of resources to align activity costs with the overall project plan. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference, featuring Paul Boustany and Mark Krasovich, to speak to the experts about becoming a Rational ClearCase power user. Get a chance to ask your questions and learn tips and tricks for using Rational ClearCase in Agile development FREE! Go There Now!
|
|
|
|
Download the IBM WebSphere Portal V6.1 beta code and learn more about the rich features and enhancements in IBM WebSphere Portal V6.1. WebSphere Portal provides a composite application or business mashup framework and the advanced tooling needed to build flexible, SOA-based solutions, and scalability to meet the needs of any size organization. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of WebSphere Extended Deployment Compute Grid, which lets you schedule, execute, and monitor batch jobs. Because online transaction processing and batch jobs execute simultaneously on the same server resources, you can avoid costly duplication of resources. Compute Grid supports job types of Java transactional batch, compute-intensive and a new type called "native execution", which enables non-Java workloads to run on distributed end points. FREE! Go There Now!
|
|
|
|
Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started. FREE! Go There Now!
|
|
|
|
Learn how to do more with your reusable assets with the free Rational Asset Manager eKit. The eKit includes demos on how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse. Plus you’ll find white papers and a Webcast that discuss the challenges of a Service Oriented Architecture and how Rational Asset Manager can provide quick and effective solutions. FREE! Go There Now!
|
|
|
|
Informix Dynamic Server (IDS) Express Edition offers outstanding online transaction processing (OLTP) database performance, while helping to simplify and automate many of the tasks associated with deploying databases for small business applications. IDS 11 further extends the ease of management and applications integration with the Admin API and Scheduler, high availability with Continuous Log Restore for backup server recovery in case of a primary server failure, and column level encryption to protect personal and company private data. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Tester for SOA Quality V7.0.1, a functional and regression testing tool that enables the creation, comprehension, modification and execution of testing GUI-less Web services. FREE! Go There Now!
|
|
|
|
Viper 2 brings a great value to developer communities including SQL, XML, PHP, Ruby, .NET and Java. You probably already know that DB2 Express-C is free for developers to develop, deploy and distribute. Viper 2 provides a variety of means that help move your application from the development stage to deployment more rapidly. This webcast shows how to best utilize the latest tools available for developing DB2 applications. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |