我有一种奇怪的东西,我真的很喜欢我的文本格式。请不要问我为什么会做这种奇怪的事情!;-)
因此,我的 PHP 脚本将所有行折叠“\n”替换为特殊符号之一,如“|”。当我将文本数据插入数据库时,PHP 脚本将所有折行替换为符号“|” 当脚本从数据库中读取文本数据时,它会替换所有特殊符号“|” 折线“\n”。
我想限制文本格式,如果每个分隔文本中使用的折线超过 2 条,它将切断折线。
这是我希望脚本格式化的文本示例:
this is text... this is text... this is text...this is text...this is text... this is text... this is text... this is text... this is text... this is text...
this is text... this is text... this is text... this is text... this is text... this is text... this is text... this is text... this is text... this is text...
我想限制格式,如:
this is text... this is text... this is text...this is text...this is text... this is text... this is text... this is text... this is text... this is text...
this is text... this is text... this is text... this is text... this is text... this is text... this is text... this is text... this is text... this is text...
因此,在第一个示例中,两个文本之间只有一个行折叠,而在第二个示例中,两个文本之间有 3 个行折叠。
如何可以替换超过 2 行折叠符号“|” 如果在文本中检测到它们?
这是我希望脚本执行的一种示例:
$text = str_replace("|||", "||", $text);
$text = str_replace("||||", "||", $text);
$text = str_replace("|||||", "||", $text);
$text = str_replace("||||||", "||", $text);
$text = str_replace("|||||||", "||", $text);
...
$text = str_replace("||||||||||", "||", $text);
$text = str_replace("|", "<br>", $text);
嗯,我有问题!当文本数据以 POST 方法发送时,这不起作用。看这个:
//REPLACING ALL LINE FOLDINGS WITH SPECIAL SYMBOL
$_POST["text"] = str_replace("\n","|",$_POST["text"]);
// REMOVING ALL LINE FOLDINGS
$_POST["text"] = trim($_POST["text"]);
// IF THERE ARE MORE THAN 3 LINE HOLDINGS - FORMAT TO 1 LINE HOLDING
$_POST["text"] = preg_replace("/\|{3,}/", "||", $_POST["text"]);
echo $_POST["text"];
这是我在 textarea 上输入的文本,在 str_replace 之后显示:
This is text 1. This is text 1. This is text 1. This is text 1. This is text 1. This is text 1. This is text 1. | | |This is text 2. This is text 2. This is text 2. This is text 2. This is text 2. This is text 2. This is text 2. | | | |This is text 3. This is text 3. This is text 3. This is text 3. This is text 3.
这是我的 PHP 和 HTML 代码:
<?
//REPLACING ALL LINE FOLDINGS WITH SPECIAL SYMBOL
$_POST["text"] = str_replace("\n","|",$_POST["text"]);
echo "1) ".$_POST["text"]."<br><br>";
// REMOVING ALL LINE FOLDINGS
$_POST["text"] = trim($_POST["text"]);
// IF THERE ARE MORE THAN 3 LINE HOLDINGS - FORMAT TO 1 LINE HOLDING
$_POST["text"] = preg_replace("/\|{3,}/", "||", $_POST["text"]);
echo "2) ".$_POST["text"]."<br><br>";
?>
<html>
<head>
<title>No title</title>
<meta name="generator" content="Namo WebEditor v5.0">
</head>
<body bgcolor="white" text="black" link="blue" vlink="purple" alink="red">
<form name="form1" method="post" action="test.php">
<p><textarea name="text" rows="8" cols="55"></textarea></p>
<p><input type="submit" name="formbutton1"></p>
</form>
<p> </p>
</body>
</html>