Frage

Ich habe eine Art seltsame Sache, dass ich wirklich nead für meinen Text Formatierung. Frag mich nicht bitte, warum ich diese seltsame Sache tat! ; -)

Also, mein PHP-Skript ersetzt alle Zeilen Faltungen „\ n“ mit einem des speacial Symbol wie „|“. Wenn ich Text einfügen Daten in der Datenbank, ersetzt der PHP-Skript alle Zeilen Faltungen mit dem Symbol „|“ und wenn das Skript Textdaten aus der Datenbank liest, ersetzt er alle Sonderzeichen „|“ mit Zeilenfaltung "\ n".

Ich möchte Textformat in der Art und Weise beschränken, dass es Linie Faltungen geschnitten wird, wenn mehr als 2 Zeilen Faltungen in jedem Trenn Texte verwendet werden.

Hier ist das Beispiel des Textes, den ich das Skript Format will:

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

Ich möchte restict Format wie:

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

Also, im ersten Beispiel gibt es nur eine Linie zwischen zwei Texten und auf dem zweiten Beispiel Falten gibt es 3 Linie Faltungen zwischen zwei Texten.

Wie kann es möglich sein, mehr als 2 Zeilen Faltungen Symbole ersetzen „|“ wenn sie auf dem Text erkannt werden?

Dies ist eine Art von Beispiel, das ich das Skript zu tun:

    $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, ich habe Probleme! Funktionierts nicht, wenn TEXT DATA IN POST-Methode gesendet. Blick auf diese:

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

Hier ist der Text I-Eingang auf Textfeld und nach dem str_replace zeigt es diese:

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.

Hier ist meine PHP und 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>&nbsp;</p>
</body>

</html>
War es hilfreich?

Lösung

Es scheint ein guter Ort, um einen regulären Ausdruck zu verwenden:

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

In Englisch: "Ersetzen Sie 3 oder mehr | Zeichen mit ||"

Andere Tipps

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

    return $content;
}

$text = clean($text);

So etwas wie eine Loop-Funktion

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top