Notice: Undefined variable: script_path in /home4/pathosca/public_html/jpwillard.com/tools/regex/index.php on line 5
Regex

Regex

Performs a search and replace operation using PHP Perl-compatible regex (see example). This tool requires that you know the regex syntax.

Source code (hide)

Global files are not shown below.

Table of contents
  1. index.php

index.php

back to toc
<?php
/* globals, templates */
$locals = array (
    
'title'       => 'Regex',
    
'description' => 'Performs a search and replace operation using <a href="http://www.regular-expressions.info/php.html" title="tutorial on PHP regex">PHP Perl-compatible regex</a> (<a href="' $script_path '?isExample=1" title="example">see example</a>). This tool requires that you know the regex syntax.',
    
'files'       => array('index.php')
);
include(
'../globals/globals.php');

/* watch for form submission */
if($_POST) {
    
/* variables */
    
$input = array(
        
'search'    => htmlspecialchars(stripslashes($_POST['search'])),
        
'replace'   => htmlspecialchars(stripslashes($_POST['replace'])),
        
'text'      => htmlspecialchars(stripslashes($_POST['text'])),
        
'caseSensitive' => $_POST['caseSensitive'],
        
'multiline' => $_POST['multiline'],
        
'dotAll'    => $_POST['dotAll'],
        
'diff'      => $_POST['diff'],
        
'example'   => $_GET['example']
    );

    
// example
    
if($input['example']) {
        
$input = array(
            
'search'  => 'foo',
            
'replace' => 'blah',
            
'text'    => 'blah blah foo blah'
        
);
    }

    
/* errorcheck */
    // missing fields
    
if(empty($input['search']) or empty($input['text'])) {
        echo 
'<p class="fail">Please provide a search pattern and text.</p>';
    }
    else {
        
/* escape delimiter */
        
$regex['search'] = preg_replace('/\//','\/',$input['search']);

        
/* create regex */
        
$regex['search'] = '/(' $regex['search'] . ')/';
        if(!
$input['caseSensitive']) {
            
$regex['search'] = $regex['search'] . 'i';
        }
        if(
$input['multiline']) {
            
$regex['search'] = $regex['search'] . 'm';
        }
        if(
$input['dotAll']) {
            
$regex['search'] = $regex['search'] . 's';
        }

        
/* filter text */
        
$output['text'] = preg_replace($regex['search'],$input['replace'],$input['text']);
        
        
/* create diff */
        
if($input['diff']) {
            
$diff['search']  = $regex['search'];
            
$diff['replaceOld'] = '<del>$1</del>';
            
$diff['replaceNew'] = '<ins>' $input['replace'] . '</ins>';
            
$diff['textOld'] = preg_replace($diff['search'],$diff['replaceOld'],$input['text']);
            
$diff['textNew'] = preg_replace($diff['search'],$diff['replaceNew'],$input['text']);
        }
    }
}

/* output */
// debug
gDebug(get_defined_vars());

// diff
if($input['diff']) {
    
?>
    <h2>Changes (diff)</h2>
    <table>
        <tr>
            <th>Input text</th>
            <th>Filtered text</th>
        </tr>
        <tr>
            <td valign="top" style="width:45%;">
                <pre style="overflow:auto; width:95%;"><?php echo $diff['textOld']; ?></pre>
            </td>
            <td valign="top" style="width:45%;">
                <pre style="overflow:auto; width:95%;"><?php echo $diff['textNew']; ?></pre>
            </td>
        </tr>
    </table>
    <?php
}

// result
if($output['text']) {
    
?>
    <h2>Processed result</h2>
    <strong>search:</strong> <?php echo $regex['search']; ?><br />
    <strong>replace:</strong> <?php echo $regex['replace']; ?><br />
    <textarea
        rows="7"
        cols="100"
        readonly="readonly"><?php echo $output['text']; ?></textarea>
    <?php
}

// input form
?>
<h2>Input form</h2>
<form action="index.php" method="post">
    <p class="instruction">Write a regex search pattern in the first box (without delimiters) and the replace pattern in the second. For example, "(colo)u?(r)" and "$1$2" without quotations will replace any instance of "colour" with "color". Enter the text you want to execute the patterns on in the third box.</p>
    <label for="search">Search:</label><br />
    <textarea
        id="search"
        name="search"
        rows="12"
        cols="4"
        style="height:5em;"><?php echo $input['search']; ?></textarea><br />
    <label for="replace">Replace:</label><br />
    <textarea
        id="replace"
        name="replace"
        rows="12"
        cols="4"
        style="height:5em;"><?php echo $input['replace']; ?></textarea><br />
    <label for="text">Text:</label>
    <textarea
        id="text"
        name="text"
        rows="12"
        cols="12"><?php echo $input['text']; ?></textarea><br />
    <input type="submit" value="Execute patterns" /><br />
    <input type="reset" value="Reset form" />

    <fieldset>
        <legend>Extended options</legend>
        <input
            type="checkbox"
            id="caseSensitive"
            name="caseSensitive"
            <?php gCheckBox($input['caseSensitive']); ?>
        />
        <label for="caseSensitive">case sensitive</label><br />
        <input
            type="checkbox"
            id="multiline"
            name="multiline"
            <?php gCheckBox($input['multiline']); ?>
        />
        <label for="multiline">^$ match every line (multiline mode)</label><br />
        <input
            type="checkbox"
            id="dotAll"
            name="dotAll"
            <?php gCheckBox($input['dotAll']); ?>
        />
        <label for="dotAll">. matches newlines</label><br /><br />

        <input
            type="checkbox"
            id="diff"
            name="diff"
            <?php gCheckBox($input['diff']); ?>
        />
        <label for="diff">show changes (diff)</label><br />
        <?php gDebugOption(); ?>
    </fieldset>
</form>

<?php
/* globals, templates */
makeFooter();
?>

Input form

Write a regex search pattern in the first box (without delimiters) and the replace pattern in the second. For example, "(colo)u?(r)" and "$1$2" without quotations will replace any instance of "colour" with "color". Enter the text you want to execute the patterns on in the third box.







Extended options





This tool is written and copyright by Jesse Plamondon-Willard (Pathoschild). You may freely use, distribute, and modify this script in any way and for any purpose, so long as you cite the above name as original author.