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

所以,我的PHP脚本替换所有线弯折的“\ n”与像speacial标志之一“|”。当我插入文本数据的数据库,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...

欲restict格式如下:

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("||||||||||", "||", $text);

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

HM,我有问题!这不工作时,文本数据派去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的文本I输入和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>&nbsp;</p>
</body>

</html>
有帮助吗?

解决方案

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

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

在英语: “与|替换3点或更多的字符||

其他提示

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

    return $content;
}

$text = clean($text);

像一个回路功能

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top