PHP單文件實現(xiàn)代碼去行首尾空格和去空行
示例代碼
<?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="提交發(fā)送" onclick="loadcha('xid')"> <div id="tipx"></div> </form> </body> </html>
知識補充
除了上文的方法,小編還為大家整理了一些PHP去除代碼空行的方法,希望對大家有所幫助
去除字符串兩邊的空格、空字符串和換行符:
使用trim()函數(shù)去除字符串兩邊的空格和空字符串,例如:
$str = " Hello World! "; $trimmed = trim($str); echo $trimmed;
使用preg_replace()函數(shù)去除字符串中的空格、空字符串和換行符,例如:
$str = " Hello\nWorld! "; $trimmed = preg_replace('/^\s+|\s+$/m', '', $str); echo $trimmed;
使用str_replace()函數(shù)去除字符串中的空格、空字符串和換行符,例如:
$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里有連續(xù)的空行或tab給正則過濾掉> ?>
php清除html,空格,換行,提取純文字的方法:
方法一:
function DeleteHtml($str) { $str = trim($str); //清除字符串兩邊的空格 $str = preg_replace("/\t/","",$str); //使用正則表達式替換內(nèi)容,如:空格,換行,并將替換為空。 $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); //返回字符串 }
調(diào)用方法
DeleteHtml($str);
$str
為需要清除的頁面字符串
方法二:
去除字符串內(nèi)部的空行:
$str = preg_replace("/(s*?r?ns*?)+/","n",$str);
去除全部的空行,包括內(nèi)部和頭尾:
$str = preg_replace('/($s*$)|(^s*^)/m', '',$str);
到此這篇關于PHP單文件實現(xiàn)代碼去行首尾空格和去空行的文章就介紹到這了,更多相關PHP單文件去空行內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
php源碼分析之DZX1.5字符串截斷函數(shù)cutstr用法
這篇文章主要介紹了php源碼分析之DZX1.5字符串截斷函數(shù)cutstr用法,實例分析了DZX1.5中cutstr函數(shù)實現(xiàn)字符串截取的使用技巧,需要的朋友可以參考下2015-06-06淺析PHP substr,mb_substr以及mb_strcut的區(qū)別和用法
本篇文章是對PHP中的substr,mb_substr以及mb_strcut區(qū)別和用法進行了詳細的分析介紹,需要的朋友參考下2013-06-06如何使用Laravel Eloquent來開發(fā)無限極分類
在網(wǎng)上商城上,我們經(jīng)??梢钥吹蕉嗉壏诸?、子分類、甚至無限極分類。本文將向你展示如何優(yōu)雅的通過 Laravel Eloquent 將其實現(xiàn)。2021-05-05PHP中preg_match函數(shù)正則匹配的字符串長度問題
這篇文章主要介紹了PHP中preg_match函數(shù)正則匹配的字符串長度問題,如果你也遇到了preg_match正則提取內(nèi)容時總是空白或提取不到的話,那就可能是遇到了這個問題啦,需要的朋友可以參考下2015-05-05