Home > Article > Backend Development > (10) PHP regular expression learning ---- making test tools
1. Reason:
Make a PHP testing tool so that you can directly test the regular rules. This tool can be developed in the future.
2. Code:
index.php
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css"> <link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"> <script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script> <script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> </head> <body> <div style="padding: 30px 300px 10px;" id = "content"> <h2>正则表达式测试</h2> <br /> <form class="bs-example bs-example-form" role="form"> <div class="row"> <div class="col-lg-9"> <form role="form"> <div class="form-group"> <label for="name">源文本</label> <textarea class="form-control" rows="9" id = "textContent"></textarea> </div> </form> </div> </div> <div class="row"> <div class="col-lg-9"> <label for="name">正则表达式</label> <div class="input-group"> <input type="text" class="form-control" id = "regularExpression" > <span class="input-group-btn"> <button class="btn btn-default" type="button" onclick = "detectionRegularity()" > 检测 </button> </span> </div><!-- /input-group --> </div><!-- /.col-lg-6 --> </div><!-- /.row --> <br /> <div class="row"> <div class="col-lg-9"> <form role="form"> <div class="form-group"> <label for="name">正则结果</label> <textarea class="form-control" rows="3" id = "regexResult"></textarea> </div> </form> </div> </div> </form> </div> <script> //检测正则 function detectionRegularity(){ var regularExpression = $("#regularExpression").val(); var textContent = $("#textContent").val(); $.ajax({ url: 'testRegular.php', type:'post', dataType:'json', data:{ 'regularExpression':regularExpression, 'textContent':textContent }, success:function(data){ var showContent = ""; for(var eachItem in data){ showContent += data[eachItem] + "\n"; } $("#regexResult").html(showContent); }, error:function(){ $("#regexResult").html("无结果"); } }); } </script> </body> </html>
<?php $regularExpression = $_POST['regularExpression']; $textContent = $_POST['textContent']; if (preg_match_all ($regularExpression, $textContent, $result)){ if ($result[2]){ $regexResult = $result[2]; echo json_encode($regexResult); } }
Copyright statement: The original articles on this blog are welcome to be reprinted. Friends who reprint should indicate the source. Thank you all.
The above has introduced (10) PHP regular expression learning-the production of test tools, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.