Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> PHP: operacje na stringu
-James-
post
Post #1





Goście







Witam

Mam maly formularz, typu:
Kod
<form action="page.php" method="post">
    <textarea type="text" name="inputArea"></textarea>
    <p><input type="submit" name="submitButton" /></p>
</form>

po kliknieciu na przycisk zgodnie z przypuszczeniem pod $_POST['inputArea'] mam tresc wpisana do pola tekstowego. Chcialem ten tekst podzielic na linijki, tak aby w kazdym elemencie tablicy byla odobna linijka wpisanego tekstu:
Kod
$inputArea = $_POST['inputArea'];
$arrayOfLines = explode('\n',$inputArea);


i okazuje sie, ze nie jest tak pieknie, bo w zmiennej $inputArea tekst wcale nie jest oddzielono '\n', zamiast tego sa spacje! (IMG:http://forum.php.pl/style_emoticons/default/sadsmiley02.gif)
Wie ktos moze jak to rozwiazc? Bede bardzo wdzieczny! (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
fiszol
post
Post #2





Grupa: Zarejestrowani
Postów: 453
Pomógł: 16
Dołączył: 25.05.2004
Skąd: Gorzów Wlkp.

Ostrzeżenie: (0%)
-----


spróbuj \r\n
Go to the top of the page
+Quote Post
crash
post
Post #3





Grupa: Przyjaciele php.pl
Postów: 2 196
Pomógł: 2
Dołączył: 17.01.2004
Skąd: Sosnowiec

Ostrzeżenie: (0%)
-----


  1. <?php
  2. $inputArea = $_POST['inputArea'];
  3. $arrayOfLines = explode(&#092;"n\",$inputArea);
  4. ?>

Przy znakach specjalnych ważne są cudzysłowia...
Go to the top of the page
+Quote Post
-James-
post
Post #4





Goście







Niestety nie pomogło (IMG:http://forum.php.pl/style_emoticons/default/sad.gif)
Go to the top of the page
+Quote Post
-Guest-
post
Post #5





Goście







Tzn nie pomogło \r\n, ale "\n" juz poszło (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)

Bardzo dziękuję!:-)
Go to the top of the page
+Quote Post
-James-
post
Post #6





Goście







Mam jeszcze jedno pytanko...
po zrobieniu

Kod
$arrayOfLines = explode("\n",$textIn);


dla na przykład takiego tekstu:
"Pierwsza linijka

Trzecia linijka"

Otrzymuję tablicę trzyelementową, a co się znajduje w $arrayOfLines[1] ?

bo gdy daję taką pętle:
Kod
do {
    $element = $arrayOfLines[$j++];
    $tmpArray[$i++] = $element;
}while($element != "");


aby wczytał wszystkie linii do linii pustej to to nie idzie (IMG:http://forum.php.pl/style_emoticons/default/sad.gif) i wychodzi z pętli dopiero po przetworzeniu całego stringa (IMG:http://forum.php.pl/style_emoticons/default/sad.gif)
Go to the top of the page
+Quote Post
Liko
post
Post #7





Grupa: Zarejestrowani
Postów: 436
Pomógł: 6
Dołączył: 8.11.2003
Skąd: Szczecin

Ostrzeżenie: (0%)
-----


  1. <?php
  2. do {
  3. $element = $arrayOfLines[$j++];
  4. $tmpArray[$i++] = $element;
  5. } while( !empty( $element ) );
  6. ?>


Może tak ?
Go to the top of the page
+Quote Post
James
post
Post #8





Grupa: Zarejestrowani
Postów: 18
Pomógł: 0
Dołączył: 17.05.2005
Skąd: Koło / Poznań

Ostrzeżenie: (0%)
-----


Niestety nie pomogło...(IMG:http://forum.php.pl/style_emoticons/default/sad.gif)
Go to the top of the page
+Quote Post
Liko
post
Post #9





Grupa: Zarejestrowani
Postów: 436
Pomógł: 6
Dołączył: 8.11.2003
Skąd: Szczecin

Ostrzeżenie: (0%)
-----


  1. <?php
  2. if( isset( $_POST['sent'] ) ) {
  3. $arrayOfLines = explode( &#092;"n\", $_POST['inputArea'] );
  4. foreach( $arrayOfLines as $valueLine ) {
  5. $valueLine = str_replace( array( &#092;"n\", \"r\" ), \"\", $valueLine );
  6. if( empty( $valueLine ) ) break;
  7. print $valueLine.'<br />';
  8. }
  9. } else {
  10. ?>
  11. <form action=\"<?=$_SERVER['PHP_SELF'];?>\" method=\"post\">
  12.  <textarea type=\"text\" name=\"inputArea\"></textarea>
  13.  <input type=\"submit\" name=\"sent\" />
  14. <?php } ?>
Go to the top of the page
+Quote Post
James
post
Post #10





Grupa: Zarejestrowani
Postów: 18
Pomógł: 0
Dołączył: 17.05.2005
Skąd: Koło / Poznań

Ostrzeżenie: (0%)
-----


Zadziałało! (IMG:http://forum.php.pl/style_emoticons/default/biggrin.gif) dziękuję! (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 9.10.2025 - 14:04