All pastes #180361 Raw Edit

Unnamed

public text v1 · immutable
#180361 ·published 2006-09-23 02:13 UTC
rendered paste body
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
	<title>Regexp Test</title>
</head>

<body>
<h1>Reg Exp Test</h1>
<h2>Form</h2>
<form method="post"  action="<?php echo $_SERVER['self']; ?>">
<h3>Haystack:</h3>
<textarea name="haystack" cols="50" rows="10"><?php echo $_POST['haystack']; ?></textarea>
<br/>
<h3>Needle:</h3>
<input type="text" value="<?php echo $_POST['needle']; ?>" name="needle" size="30" />
<br/>
<input type="submit" name="submit" value="Check" />
</form>
<h2>Result</h2>
<?php
if(strlen($_POST['needle']) > 0){
	echo "<strong>Needle:</strong><br/> <em>" . $_POST['needle'] . "</em><br/>";
	echo "<strong>Haystack:</strong><br/><em>" . nl2br($_POST['haystack']) . "</em><br/>";
    echo "<h3>Matches</h3>";
    $keepLooping = true;
    $returnStack = "";
    $haystack = $_POST['haystack'];
    while($keepLooping){
        $returnR = buildMatch($_POST['needle'],$haystack,$returnStack);
        $keepLooping = $returnR['keepLooping'];
        $returnStack = $returnR['returnStack'];
        if(!$keepLooping){
            break;
        }
        $haystack = $returnR['haystack'];
    }
    echo "<p>". nl2br($returnStack) . "</p>";
?>
<?php
}
function buildMatch($needle,$haystack,$returnStack){
	$matchLength = ereg ( $needle, $haystack, $regs );
    if($matchLength){
    /*
        echo "<pre>";
        print_r($matchLength);
        echo "\r\n";
        print_r($regs);
        echo "</pre>";
    */
        $strpos = strpos($haystack,$regs[0]);
    //    echo $strpos;
        $returnStack .= 
        		substr ($haystack, 0, $strpos) 
        		. "<font color='red'>" 
        		. substr ($haystack,$strpos,$matchLength) 
        		. "</font>";
        $returnR['haystack'] = substr($haystack,($strpos + $matchLength));
        $returnR['returnStack'] = $returnStack;
        $returnR['keepLooping'] = true;
         return $returnR;
    }else{
        $returnStack .= $haystack;
        $returnR['returnStack'] = $returnStack;
        $returnR['keepLooping'] = false;
        return $returnR;
    }
}
?>

</body>
</html>