PHP文本格式化:检测连续的几个符号

iln*_*777 0 php formatting line folding str-replace

我有一种奇怪的事情,我真的需要我的文本格式化.不要问我为什么我做了这个奇怪的事情!;-)

所以,我的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...
Run Code Online (Sandbox Code Playgroud)

我想重写格式,如:

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...
Run Code Online (Sandbox Code Playgroud)

因此,在第一个例子中,在2个文本之间只有一个折叠线,而在第二个例子中,在2个文本之间有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);
Run Code Online (Sandbox Code Playgroud)

HM,我有问题!当文本数据在后方法中出现时,这不起作用.看这个:

//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"];
Run Code Online (Sandbox Code Playgroud)

这是我在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.
Run Code Online (Sandbox Code Playgroud)

这是我的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>&nbsp;</p>
</body>

</html>
Run Code Online (Sandbox Code Playgroud)

Cha*_*rch 5

似乎是使用正则表达式的好地方:

$text = preg_replace('/\|{3,}/', '||', $text);
Run Code Online (Sandbox Code Playgroud)

英文:"用3个或更多个|字符替换||"