Skip to content

Instantly share code, notes, and snippets.

@twesolowski
Created December 20, 2013 13:19
Show Gist options
  • Select an option

  • Save twesolowski/8054649 to your computer and use it in GitHub Desktop.

Select an option

Save twesolowski/8054649 to your computer and use it in GitHub Desktop.
preg_match php one-page tool with history
<?php
$subject = isset($_POST['subject']) ? $_POST['subject'] : null;
$pattern = isset($_POST['pattern']) ? $_POST['pattern'] : null;
$historyFile = "preg_match_history.html";
session_start();
if(!isset($_SESSION['lastPattern'])){
$_SESSION['lastPattern'] = null;
}
$matchesPregMatch = null;
$matchesPregMatchAll = null;
if($subject && $pattern){
if(preg_match($pattern, $subject, $matchesPregMatch) !== false){
preg_match_all($pattern, $subject, $matchesPregMatchAll);
}
if($_SESSION['lastPattern'] !== $pattern){
file_put_contents($historyFile, "<i>".date("Y-m-d H:i:s")."</i><b> ".$pattern."</b><br />\n".@file_get_contents($historyFile));
}
$_SESSION['lastPattern'] = $pattern;
}
?>
<html>
<head>
<style>
body{
padding:0;
margin:0;
}
textarea{
width:100%;
margin:0px 0 ;
border-radius:10px;
border:1px solid #f0f0f0;
}
textarea:focus, input:focus{
background-color:#f0f0f0;
}
#pattern{
width:100%;
padding:1% 1%;
font-size:16px;
letter-spacing:1px;
margin:10px 0;
border-radius:10px;
border:1px solid #f0f0f0;
}
button{
width:100%;
margin:20px 0;
padding:2%;
}
#history{
width:100%;
height:100px;
position:fixed;
left:0;
bottom:0;
}
.matches{
float:left;
width:50%;
}
.matches.all{
float:right;
}
</style>
<script>
window.onload = function() {
var input = document.getElementById("pattern").focus();
}
</script>
</head>
<body>
<form method="post">
<textarea id="subject" name="subject" ><?=$subject ;?></textarea>
<input type="text" id="pattern" name="pattern" value="<?=$_SESSION['lastPattern'] ;?>" />
<button type="submit">preg_match</button accesskey="return">
<div class="matches">
<pre><?php var_dump($matchesPregMatch); ?></pre>
</div>
<div class="matches all">
<pre><?php var_dump($matchesPregMatchAll); ?></pre>
</div>
</form>
<iframe id="history" src="<?=$historyFile;?>"></iframe>
</body>
</html>
@xeoncross
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment