Domanda

ho una specie di cosa strana che ho davvero nead per il mio formattazione del testo. Non chiedo a me piace perché ho fatto questa cosa strana! ; -)

Quindi, il mio script PHP sostituisce tutte le piegature linea "\ n" con uno dei simboli che andavano come "|". Quando inserisco dati di testo al database, lo script PHP sostituisce tutte le piegature di linea con il simbolo "|" e quando lo script legge i dati di testo dal database, sostituisce tutti i simboli speciali "|" con la linea di piegatura "\ n".

Voglio limitare formato di testo in modo che taglierà piegature linea se ci sono più di 2 piegature di linea utilizzati in ogni testi separano.

Ecco l'esempio del testo che voglio lo script per formato:

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

Voglio formato restict come:

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

Così, il primo esempio c'è solo una linea di piegatura fra 2 testi e al secondo esempio ci sono 3 piegature linea tra le 2 testi.

Come può essere possibile sostituire più di 2 piegature linea simboli "|" se vengono rilevati sul testo?

Questo è un tipo di esempio che voglio lo script per fare:

    $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, ho problemi! Questo non funziona quando il testo dati vengono inviati in POST METODO. Guarda a questo:

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

Ecco il metodo di scrittura che sulla textarea e dopo la str_replace è mostrare questo:

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.

Ecco il mio codice PHP e 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>
È stato utile?

Soluzione

Mi sembra un buon posto per usare un'espressione regolare:

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

In inglese: "Sostituire 3 o più caratteri | con ||"

Altri suggerimenti

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

    return $content;
}

$text = clean($text);

Qualcosa di simile a una funzione di loop

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top