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";
/*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.