Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

invert some text

Status
Not open for further replies.

crazybeans128

Programmer
Aug 5, 2003
79
US
So I have this form people fill out, and when I write it into the file, I want the content of a text box to be inversed.

To be more specific, you have, for instance, 3 lines.

Line 1
Line 2
Line 3

I want to then invert these into

Line 3
Line 2
Line 1

and then I will write them into the file.

 
Did you check the manual for string functions to see if there is something that can do this for you?
 
You can use the [blue]explode()[/blue] function, to put the lines of text into an array and then read it backwards.

Code:
$lines=explode("\n",$mytext);
$numberoflines=count($lines);
for ($i=$numberoflines;$i>=0'$i--){
$inverted.= $lines[$i];
}

then save [red]$inverted[/red] to file.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
perhaps even easier to use rsort on the resultant array?
Code:
$lines = explode("\n", $mytext);
rsort($lines);
fopen ($filename, "w");  
foreach ($lines as $line);
  fwrite($fh, $line."\n");
endforeach;
fclose ($fh);
 
oops - should be a colon at the end of the foreach line, not a semi-colon.
 
I thought of something like that before, but the only text it would output was "array". Maybe I had something in my syntax wrong, but I'll try anyway. Thanks.

 
That was probably because unlike standard variables, arrays cannot be echoed just by using
Code:
echo $array;
This will only output [green]Array[/green]. Telling you its an array.
You have to specify which item in the array you want to echo.

You can use [blue]print_r()[/blue] to print out the entire contents of the array.

Code:
[blue]print_r([/blue][red]$array[/red][blue]);[/blue]
this will output something like:
[green]
Array
(
    [0] => This is the first line
    [1] => this is the second line
    [2] => this is the third line
)
[/green]
But this is just used for debugging purposes. Because it outputs it preformatted.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
So I figured out echoing the lines backwards like I wanted, but now I am having another problem. When the data comes through like the following:
Line 1

Line 2

when it gets written to the file I have what looks like this:
Line 2
Line 1


I don't know the best way to show this, but it adds the spaces I have between lines to the end of the line. I have an idea to avoid this, but to still show spaces between my paragraphs when it comes back to my main page. And ideas in the mean time?

 
If you use the [blue]sort[/blue] function, it displays that behavior, however if you use my code, the spaces between the lines are preserved where the are.

Code:
Using this text:
[blue]
This is Line 1

This is Line 2

This is Line 3
[/blue]

In the Array it looks like this
Array
(
    [0] => This is Line 1
    [1] => 
    [2] => This is Line 2
    [3] => 
    [4] => This is Line 3
)
Using the sort function from PHP it comes back like this:

Array
(
    [0] => This is Line 3
    [1] => This is Line 2
    [2] => This is Line 1
    [3] => 
    [4] => 
)


Using my original suggestion it looks like this:


Array
(
    [0] => This is Line 3
    [1] => 
    [2] => This is Line 2
    [3] => 
    [4] => This is Line 1
)

In Other words my original suggestion does exactly what you want and preserves the empty lines between the text.




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Ah thank you. I have working system now. I have only one more problem, this one not involving the array. The text that is written into the file is just fine unless I have an apostrophie ' in there. then i get this \' in the middle of my sentence. I have determind as much as that the code that reads this sees it as some sort of markup, but it puts it into my file, and reads it back exactly like that.

 
Okay, wait, nevermind, I discovered a method, and a very simple one at that. Success! Thanks for all you help guys.

 
You are Welcome, glad we could help.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top