欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP單文件實現(xiàn)代碼去行首尾空格和去空行

 更新時間:2024年03月13日 11:12:14   作者:YUJIANYUE  
這篇文章主要為大家詳細介紹了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).&#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里有連續(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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論