京东6.18大促主会场领京享红包更优惠

 找回密码
 立即注册

QQ登录

只需一步,快速开始

PHP单文件实当代码去行首尾空格和去空行

2024-11-4 06:37| 发布者: c2688| 查看: 124| 评论: 0

摘要: 示例代码 [code]<?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',

示例代码

[code]<?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>[/code]

知识增补

除了上文的方法,小编还为各人整理了一些PHP去除代码空行的方法,希望对各人有所帮助

去除字符串两边的空格、空字符串和换行符:

使用trim()函数去除字符串两边的空格和空字符串,比方:

[code]$str = " Hello World! "; $trimmed = trim($str); echo $trimmed;[/code]

使用preg_replace()函数去除字符串中的空格、空字符串和换行符,比方:

[code]$str = " Hello\nWorld! "; $trimmed = preg_replace('/^\s+|\s+$/m', '', $str); echo $trimmed; [/code]

使用str_replace()函数去除字符串中的空格、空字符串和换行符,比方:

[code]$str = " Hello\nWorld! "; $trimmed = str_replace(array("\n","\r","\t"),"",$str); echo $trimmed;[/code]

去掉多余的空行

[code]<?php $str="i am a book\n\n\n\n\nmoth"; //去除全部的空格和换行符 echo preg_replace("/[\s]{2,}/","",$str).&#39;<br>&#39;; //去除多余的空格和换行符,只保存一个 echo preg_replace("/([\s]{2,})/","\\1",$str); //去除多余的空格或换行 $text = preg_replace("/(\r\n|\n|\r|\t)/i", &#39;&#39;, $text); $lastSeveralLineContentsArr = preg_replace("/([ |\t]{0,}[\n]{1,}){2,}/","",$lastSeveralLineContentsArr); //对Html里有连续的空行或tab给正则过滤掉> ?>[/code]

php扫除html,空格,换行,提取纯笔墨的方法:

方法一:

[code]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); //返回字符串 }[/code]

调用方法

[code]DeleteHtml($str);[/code]

[code]$str[/code]为必要扫除的页面字符串

方法二:

去除字符串内部的空行:

[code]$str = preg_replace("/(s*?r?ns*?)+/","n",$str);[/code]

去除全部的空行,包括内部和头尾:

[code]$str = preg_replace('/($s*$)|(^s*^)/m', '',$str);[/code]

到此这篇关于PHP单文件实当代码去行首尾空格和去空行的文章就先容到这了,更多相关PHP单文件去空行内容请搜刮脚本之家从前的文章或继续浏览下面的相关文章希望各人以后多多支持脚本之家!


来源:https://www.jb51.net/program/317630blr.htm
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
关闭

站长推荐上一条 /6 下一条

QQ|手机版|小黑屋|梦想之都-俊月星空 ( 粤ICP备18056059号 )|网站地图

GMT+8, 2025-7-1 21:53 , Processed in 0.031118 second(s), 19 queries .

Powered by Mxzdjyxk! X3.5

© 2001-2025 Discuz! Team.

返回顶部