一.原因:
做一个PHP的测试工具,这样可以直接测试正则。以后还可以发展这一工具。
二.代码:
index.php
<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> <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>
<?php $regularExpression = $_POST['regularExpression']; $textContent = $_POST['textContent']; if (preg_match_all ($regularExpression, $textContent, $result)){ if ($result[2]){ $regexResult = $result[2]; echo json_encode($regexResult); } }
版权声明:本博客原创文章欢迎转载,请转载的朋友最好注明出处,谢谢大家。
以上就介绍了(十)PHP正则表达式学习----测试工具的制作,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。