示例代码
<?php if($_GET["x"] == "cha"){ $tips = isset($_POST['tips']) ? $_POST['tips'] : ''; $tips = preg_replace('/^\s+|\s+$/m', "\r\n", $tips);//去首尾空格 $tips = preg_replace('/(\r|\n)+/m', "\r\n", $tips);//去首尾空格 echo "<h2><strong>代码去首尾空格+空行</strong>:</h2>\r\n"; echo "<textarea>".Trim($tips)."</textarea>"; exit(); } ?> <!DOCTYPE html> <html> <head> <title>代码去首尾空格+空行</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0"> <meta name="apple-mobile-web-app-capable" content="yes" /> <script type="text/javascript"> console.log("问题反馈电话:","15058593138"); console.log("问题反馈邮件:","admin@12391.net"); function $(objId){ return document.getElementById(objId); } function loadcha(xid) { var xmlhttp; var Stxt= "nums=aa"; Stxt+="&tips="+ encodeURIComponent($("tips").value); //$("tips").innerHTML = "正在加载..."; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { var btxt = xmlhttp.response; if(btxt == "err01"){ $("tipx").innerHTML = "!"; return false;} $('tipx').innerHTML = xmlhttp.response; } } xmlhttp.open("POST", "?x=cha&tt="+Math.random(), true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.send(Stxt); } </script> <style> div,#tipx{display:block;width:99.7%;border:0;margin-top:5px;} textarea{display:block;width:99.7%;border:1px solid #ccc;height:160px;} table{margin:20px auto;border-left:1px solid #a2c6d3;border-top:3px solid #0180CF;font-size:12px;width:99.7%;} table td{border-right:1px solid #a2c6d3;border-bottom:1px solid #a2c6d3;padding:2px;word-wrap:break-word;word-break:break-all;} td{min-width:30px;max-width:490px;} #submit{ height:35px;} </style> </head> <body> <form class="form" id="form" method="POST" act="?act=cha" > <h3>代码去首尾空格+空行</h3> <textarea id="tips"> 1 2 2 3 制表符 我你 </textarea> <input type="button" id="submit" value="提交发送" onclick="loadcha('xid')"> <div id="tipx"></div> </form> </body> </html>
知识补充
除了上文的方法,小编还为大家整理了一些PHP去除代码空行的方法,希望对大家有所帮助
去除字符串两边的空格、空字符串和换行符:
使用trim()函数去除字符串两边的空格和空字符串,例如:
$str = " Hello World! "; $trimmed = trim($str); echo $trimmed;
使用preg_replace()函数去除字符串中的空格、空字符串和换行符,例如:
$str = " Hello\nWorld! "; $trimmed = preg_replace('/^\s+|\s+$/m', '', $str); echo $trimmed;
使用str_replace()函数去除字符串中的空格、空字符串和换行符,例如:
$str = " Hello\nWorld! "; $trimmed = str_replace(array("\n","\r","\t"),"",$str); echo $trimmed;
去掉多余的空行
<?php $str="i am a book\n\n\n\n\nmoth"; //去除所有的空格和换行符 echo preg_replace("/[\s]{2,}/","",$str).'<br>'; //去除多余的空格和换行符,只保留一个 echo preg_replace("/([\s]{2,})/","\\1",$str); //去除多余的空格或换行 $text = preg_replace("/(\r\n|\n|\r|\t)/i", '', $text); $lastSeveralLineContentsArr = preg_replace("/([ |\t]{0,}[\n]{1,}){2,}/","",$lastSeveralLineContentsArr); //对Html里有连续的空行或tab给正则过滤掉> ?>
php清除html,空格,换行,提取纯文字的方法:
方法一:
function DeleteHtml($str) { $str = trim($str); //清除字符串两边的空格 $str = preg_replace("/\t/","",$str); //使用正则表达式替换内容,如:空格,换行,并将替换为空。 $str = preg_replace("/\r\n/","",$str); $str = preg_replace("/\r/","",$str); $str = preg_replace("/\n/","",$str); $str = preg_replace("/ /","",$str); $str = preg_replace("/ /","",$str); //匹配html中的空格 return trim($str); //返回字符串 }
调用方法
DeleteHtml($str);
$str
为需要清除的页面字符串
方法二:
去除字符串内部的空行:
$str = preg_replace("/(s*?r?ns*?)+/","n",$str);
去除全部的空行,包括内部和头尾:
$str = preg_replace('/($s*$)|(^s*^)/m', '',$str);
到此这篇关于PHP单文件实现代码去行首尾空格和去空行的文章就介绍到这了,更多相关PHP单文件去空行内容请搜索插件窝以前的文章或继续浏览下面的相关文章希望大家以后多多支持插件窝!