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! |
Learn how you can extend modern application lifecycle management to IBM System z through the IBM Rational Software Delivery Platform (SDP). The Did you say mainframe? e-kit includes podcasts, webcasts, tutorials, white and red papers, demos, and articles designed to help ease the challenges of modernizing your enterprise. This complimentary kit for mainframe developers is a practical, how-to guide for making the most of an existing development environment, including the skills and infrastructure already in place at an established enterprise. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of WebSphere Business Modeler Advanced V6.1.1, IBM’s premier business process modeling and analysis tool for business users that offers process modeling, simulation, and analysis capabilities. IBM WebSphere Business Modeler helps you visualize, understand, and document business processes for continuous improvement. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download the latest trial version of IBM Data Studio V1.1 at no cost. IBM Data Studio is a comprehensive data management solution that helps you effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life cycle utilizing a consistent and integrated user interface. Unlike other client-side data management solutions that focus on only one aspect of the application lifecycle or database administration, Data Studio complements the Rational Software Delivery platform, providing unparalleled flexibility for a heterogeneous data server environment across platforms. FREE! Go There Now!
|
|
|
|
Download a free trial version of IBM Rational Developer for System i V7.1, which provides a complete development environment for traditional i5/OS application development. IBM Rational Developer for System i is a new eclipse-based workstation offering for i5/OS application development that provides a comprehensive Integrated Development Environment for edit/compile/debug of traditional RPG/COBOL/C/C++ i5/OS applications. FREE! Go There Now!
|
|
|
|
In this tutorial, you can learn how to install and configure the IBM Rational Asset Manager Eclipse client, explore the different views in the Asset Management perspective, learn various search techniques, work with existing assets, and submit a new asset. FREE! Go There Now!
|
|
|
|
Learn from the best! Find out how developers use Rational ClearCase to be more flexible, innovative and deliver higher quality code in the Rational ClearCase Power Users eKit. This complimentary eKit provides a collection of materials, like articles, whitepapers, and demos that can help you become a power user of Rational ClearCase. FREE! Go There Now!
|
|
|
|
Portfolio Management is about effectively managing portfolio value by aligning portfolio investments with business goals. This complimentary e-kit provides a collection of materials that can help you understand how IBM Rational enables and automates best practices for improved governance and clear visibility into portfolio and project performance across the entire IT project lifecycle. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered! FREE! Go There Now!
|
|
|
|
Learn the basics of the IBM Customer Information Control System (CICS). With a hands-on exercise, learn how to get your first CICS application up and running on your desktop using TXSeries V6.1 for Windows. The tutorial shows you how to download and install a free trial version of TXSeries V6.1. 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!
|
|
|
|
All FREE IBM® developerWorks Tools! |