views:

44

answers:

2

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("||||||||||", "||", $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>&nbsp;</p>
</body>

</html>
+5  A: 

Seems like a good place to use a regular expression:

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

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

Chad Birch
Chad Birch, I HAVE PROBLEMS! THIS DOES NOT WORK WHEN TEXT DATA IS SENT IN POST METHOD. LOOK ABOVE! I HAVE POSTED MY CODE.
ilnur777
Easy on the Caps Lock. What part isn't working? If you move your `echo` line to below the `str_replace("\n","|",$_POST["text"])` line, does it output the text correctly (with `|` in place of newlines)?
Chad Birch
I've put echo after str_replace(...) - and it outputs text correctly with all "\n" replaced with "|". But the regular expression does not replace character "|" if there are more than 2 "|". Why? :-(
ilnur777
It seems to work fine when I test it, can you edit the question and add in the text that gets output if you move the echo to after `str_replace(...)`? I'll test with the same string as you.
Chad Birch
Edited question with my full code. Try please! Thank you.
ilnur777
A: 
function clean($content) {
    $content = str_replace("||","|",$content);
    if (stripos("||",$content) !== false) {
        $content = clean($content);
    }

    return $content;
}

$text = clean($text);

Something like a loop function

Brant
This code makes en error when there is only one "|" symbol detected on a text. But nevertheless thank you! ;-)
ilnur777