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

时间:2010-04-05 20:58:33

标签: php line folding str-replace formatting

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

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

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

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

</html>

2 个答案:

答案 0 :(得分:5)

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

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

英文版:“用|”替换3个或更多||个字符

答案 1 :(得分:0)

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

    return $content;
}

$text = clean($text);

像循环函数