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

发布于 2024-08-27 23:09:50 字数 1369 浏览 5 评论 0原文

我有一种奇怪的东西,我确实需要它来进行文本格式化。请不要问我为什么要做这种奇怪的事! ;-)

因此,我的 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...

因此,在第一个示例中,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("

I have a kind of strange thing that I really nead for my text formating. Don't ask me please why I did this strange thing! ;-)

So, my PHP script replaces all line foldings "\n" with one of the speacial symbol like "|". When I insert text data to database, the PHP script replaces all line foldings with the symbol "|" and when the script reads text data from the database, it replaces all special symbols "|" with line folding "\n".

I want to restrict text format in the way that it will cut line foldings if there are more than 2 line foldings used in each separating texts.

Here is the example of the text I want the script to format:

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...

I want to restict format like:

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...

So, at the first example there is only one line folding between 2 texts and on the second example there are 3 line foldings between 2 texts.

How it can be possible to replace more than 2 line foldings symbols "|" if they are detected on the text?

This is a kind of example I want the script to do:

    $text = str_replace("|||", "||", $text);
$text = str_replace("||||", "||", $text);
$text = str_replace("|||||", "||", $text);
$text = str_replace("||||||", "||", $text);
$text = str_replace("|||||||", "||", $text);
...
$text = str_replace("

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掩耳倾听 2024-09-03 23:09:50

", "||", $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>

", "||", $text);

$text = str_replace("|", "<br>", $text);

HM, I HAVE PROBLEMS! THIS DOES NOT WORK WHEN TEXT DATA IS SENT IN POST METHOD. LOOK AT THIS:

//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"];

Here is the text I input on textarea and after the str_replace it show this:

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.

Here is my PHP and HTML code:

<?
//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>
原谅过去的我 2024-09-03 23:09:50

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

$text = preg_replace('/\|{3,}/', '||', $text);

用英语:“将 3 个或更多 | 字符替换为 ||

Seems like a good place to use a regular expression:

$text = preg_replace('/\|{3,}/', '||', $text);

In english: "Replace 3 or more | characters with ||"

美羊羊 2024-09-03 23:09:50
function clean($content) {
    $content = str_replace("||","|",$content);
    if (stripos("||",$content) !== false) {
        $content = clean($content);
    }

    return $content;
}

$text = clean($text);

类似循环函数的东西

function clean($content) {
    $content = str_replace("||","|",$content);
    if (stripos("||",$content) !== false) {
        $content = clean($content);
    }

    return $content;
}

$text = clean($text);

Something like a loop function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文