示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <?php if ( $_GET [ "x" ] == "cha" ){ $tips = isset( $_POST [ 'tips' ]) ? $_POST [ 'tips' ] : '' ; $tips = preg_replace( '/^s+|s+$/m' , "rn" , $tips ); //去首尾空格 $tips = preg_replace( '/(r|n)+/m' , "rn" , $tips ); //去首尾空格 echo "<h2><strong>代码去首尾空格+空行</strong>:rn" ; echo "<textarea>" .Trim( $tips ). "</textarea>" ; exit (); } ?> <title>代码去首尾空格+空行</title> 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); } 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;} </h2> |
代码去首尾空格+空行
知识补充
除了上文的方法,小编还为大家整理了一些PHP去除代码空行的方法,希望对大家有所帮助
去除字符串两边的空格、空字符串和换行符:
使用trim()函数去除字符串两边的空格和空字符串,例如:
1 2 3 | $str = " Hello World! " ; $trimmed = trim( $str ); echo $trimmed ; |
使用preg_replace()函数去除字符串中的空格、空字符串和换行符,例如:
1 2 3 | $str = " HellonWorld! " ; $trimmed = preg_replace( '/^s+|s+$/m' , '' , $str ); echo $trimmed ; |
使用str_replace()函数去除字符串中的空格、空字符串和换行符,例如:
1 2 3 | $str = " HellonWorld! " ; $trimmed = str_replace ( array ( "n" , "r" , "t" ), "" , $str ); echo $trimmed ; |
去掉多余的空行
1 2 3 4 5 6 7 8 9 | <?php $str = "i am a booknnnnnmoth" ; //去除所有的空格和换行符 echo preg_replace( "/[s]{2,}/" , "" , $str ). '<br>' ; //去除多余的空格和换行符,只保留一个 echo preg_replace( "/([s]{2,})/" , "\1" , $str ); //去除多余的空格或换行 $text = preg_replace("/(rn|n|r|t)/i", '', $text); $lastSeveralLineContentsArr = preg_replace( "/([ |t]{0,}[n]{1,}){2,}/" , "" , $lastSeveralLineContentsArr ); //对Html里有连续的空行或tab给正则过滤掉> ?> |
php清除html,空格,换行,提取纯文字的方法:
方法一:
1 2 3 4 5 6 7 8 9 10 11 | function DeleteHtml( $str ) { $str = trim( $str ); //清除字符串两边的空格 $str = preg_replace( "/t/" , "" , $str ); //使用正则表达式替换内容,如:空格,换行,并将替换为空。 $str = preg_replace( "/rn/" , "" , $str ); $str = preg_replace( "/r/" , "" , $str ); $str = preg_replace( "/n/" , "" , $str ); $str = preg_replace( "/ /" , "" , $str ); $str = preg_replace( "/ /" , "" , $str ); //匹配html中的空格 return trim( $str ); //返回字符串 } |
调用方法
1 | DeleteHtml( $str ); |
$str
为需要清除的页面字符串
方法二:
去除字符串内部的空行:
1 | $str = preg_replace( "/(s*?r?ns*?)+/" , "n" , $str ); |
去除全部的空行,包括内部和头尾:
1 | $str = preg_replace( '/($s*$)|(^s*^)/m' , '' , $str ); |
到此这篇关于PHP单文件实现代码去行首尾空格和去空行的文章就介绍到这了,更多相关PHP单文件去空行内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!