一.原因:
做一個PHP的測試工具,這樣可以直接測試正規。以後還可以發展這項工具。
二.程式碼:
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); } }
版權聲明:本部落格原創文章歡迎轉載,請轉載的朋友最好註明出處,謝謝大家。
以上就介紹了(十)PHP正規表示式學習----測試工具的製作,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。