Text
|
|
|
|
<?php /*
* File: regex_check.php
*
* Reguler expression syntax check for PHP and JavaScript.
*
* I use this file to make Reglar Expression works for both PHP and JavaScript.
* This file is very handy to check regex work for both server and client side,
* since I would like to use the same regex for both server and client side, if
* possible.
*
* JavaScript's regex is very limitted. You probably should take a look at reference
* to see what is supported under JavaScript RegEx.
*
* Now, it supports both ereg() and preg_match() style.
*
*
* yohgaki@hotmail.com
*/
// Init variables $posix_regex = @$HTTP_POST_VARS['posix_regex']; $pcre_regex = @$HTTP_POST_VARS['pcre_regex']; $teststr = @$HTTP_POST_VARS['teststr']; $result = 'No regular expression';
// PHP(ServerSide) regex check code if (isset($HTTP_POST_VARS['php_posix_regex']) && !empty($posix_regex)) {
if (ereg($posix_regex,$teststr,$regs)) {
$result = "Match <b>POSIX</b><br><br>n[( ) Matches]<br>n";
for ($i=0; $i < count($regs); $i++) {
$result .= $i .' = '. $regs[$i] .', ';
}
}
else {
$result = "No Match<br>n";
}
}
// PHP(ServerSide) PCRE regex check code if (isset($HTTP_POST_VARS['php_pcre_regex']) && !empty($pcre_regex)) {
if (preg_match($pcre_regex,$teststr,$regs)) {
$result = "Match <b>PCRE</b><br><br>n[( ) Matches]<br>n";
for ($i=0; $i < count($regs); $i++) {
$result .= $i .' = '. $regs[$i] .', ';
}
}
else {
$result = "No Match<br>n";
}
}
// JavaScript(ClientSide) POSIX regex style check code
// NOTE: JavaScript's regex is not actually a POSIX extended regex.
// reg = new RegExp('regex',<options>); // JavaScript supports this style.
$js_posix ='
if (window.RegExp) {
reg = new RegExp(f.posix_regex.value, "g");
if (reg.test(f.teststr.value)) {
result = "Match **POSIX**nn[( ) Matches]n";
result += "0 = " + RegExp.lastMatch + ", ";
for (i = 1; i < 10; i++) {
expr = "RegExp.$" + i;
result += i + " = " + eval(expr) + ", ";
}
alert(result);
}
else {
alert("No Match");
}
}
else {
alert("Warning! Your browser does not support JavaScript1.2");
}
'; // END JavaScript (RegEx POSIX)
// JavaScript(ClientSide) PCRE style regex check code
// NOTE: JavaScript regex is not actually a Perl Compatible regex
// reg = /regex/i; // JavaScript support this style. $js_pcre ='
if (window.RegExp) {
expr = "reg = "+ f.pcre_regex.value;
eval(expr);
if (reg.test(f.teststr.value)) {
result = "Match **PCRE**nn[( ) Matches]n";
result += "0 = " + RegExp.lastMatch + ", ";
for (i = 1; i < 10; i++) {
expr = "RegExp.$" + i;
result += i + " = " + eval(expr) + ", ";
}
alert(result);
}
else {
alert("No Match");
}
}
else {
alert("Warning! Your browser does not support JavaScript1.2");
}
'; // END JavaScript
?>
<html>
<head>
<title>JavaScript/PHP Regular Expression Check</title>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-JP">
<script type="text/javascript" language="javascript">
<!--
function POSIX_RegEx(f) {
<?php echo $js_posix ?>
}
function PCRE_RegEx(f) {
<?php echo $js_pcre ?>
}
function clear_fields(f) {
f.regex.value = "";
f.teststr.value = "";
}
//-->
</script>
</head>
<body bgcolor="#FFFFFF">
<h2><font color="#0000CC">PHP and JavaScript Regular Expression Test</font></h2>
<p>This page is for testing regular expression for both PHP and JavaScript.<b> PHP and JavaScript regular expression is some what similar,
but they differs.</b> I think form variables must be checked for both Client and Server side. AND I would like to use the same regular
expression for both Client and Server side, if it's possible. (I use my form class that uses regex to validate inputs on both client
and server side.)</p>
<p>I use this page to check if the regular expression works for both PHP and JavaScript. <br>
(You probably should check your regex with sevral browsers. Note: RegEX is supported JavaScript1.2 or later.) </p>
<form name="form" method="post" action="<?php print($HTTP_SERVER_VARS['PHP_SELF']);?>">
<table width="100%" border="1" cellspacing="1" cellpadding="2">
<tr>
<td width="13%" nowrap>
<p><font color="#0000CC"><b>Regular Expression</b></font><br>
PHP<b> preg_match()</b> and <b>JavaScript</b></p>
</td>
<td width="87%">
<input type="text" name="pcre_regex" value="<?php echo $pcre_regex ?>" size="80">
</td>
</tr>
<tr>
<td width="13%" nowrap>
<p><b><font color="#0000CC">Regular Expression</font></b><br>
PHP <b>ereg()</b> and <b>JavaScript</b></p>
</td>
<td width="87%">
<input type="text" name="posix_regex" value="<?php echo $posix_regex ?>" size="80">
</td>
</tr>
<tr>
<td width="13%" nowrap><b><font color="#0000CC">Strings test against</font></b></td>
<td width="87%">
<textarea name="teststr" cols="80" rows="10"><?php echo $teststr ?></textarea>
</td>
</tr>
<tr>
<td width="13%" nowrap><b><font color="#0000CC">PHP regex result<br>
</font></b>JavaScript Regex result is <br>
displayed on Alert Window.<b><font color="#0000CC"><br>
</font></b></td>
<td width="87%"><?php echo $result ?></td>
</tr>
</table>
<br>
<table width="100%" border="0" cellspacing="1" cellpadding="�P">
<tr>
<td width="20%">
<input type="submit" name="php_pcre_regex" value="PHP PCRE RegEx check">
</td>
<td>
<input type="button" name="js_pcre_regex" value="JavaScript PCRE RegEx check" onClick="PCRE_RegEx(form)">
(Result: ALERT window) </td>
<td>�@</td>
</tr>
<tr>
<td width="20%">
<input type="submit" name="php_posix_regex" value="PHP Posix RegEx check">
</td>
<td>
<input type="button" name="js_posix_regex" value="JavaScript Posix RegEx check" onClick="POSIX_RegEx(form)">
(Result: ALERT window) </td>
<td>�@ </td>
</tr>
</table>
<br>
<br>
</form>
<p>PHP Manual</p>
<ul>
<li><a href="http://www.php.net/manual/en/function.ereg.php">ereg()</a></li>
<li><a href="http://www.php.net/manual/en/function.preg-match.php">preg_match()</a></li>
</ul>
<p>Reference Site (Netscape.com)</p>
<ul>
<li><a href="http://developer.netscape.com/docs/manuals/communicator/jsref/corea3.htm#1151535">JavaScript Regex Syntax</a> </li>
<li><a href="http://developer.netscape.com/docs/manuals/communicator/jsref/corea3.htm#1158210">JavaScript Reference - RegEx</a></li>
<li><a href="http://developer.netscape.com/docs/manuals/communicator/jsref/index.htm">JavaScript Reference</a></li>
</ul>
</body>
</html>
|
|
|
Usage Example
|
This is complete script, so copy source and save it. That's all.
Note: No error checks done in script, so you may not see ereg(), preg_match() errors unless error_reporting = E_ALL. "track_vars" must be enabled. "magic_quote" must be disabled. You need to change code a little if you are using magic_quote.
|
|
|
Rate This Script
|
|
|
|