Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP][TXT] Czytanie z pliku
Puszkin4717
post
Post #1





Grupa: Zarejestrowani
Postów: 16
Pomógł: 0
Dołączył: 18.03.2007

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


Mam taki skrypt i wszystko spoko działa ale chce zamienić żeby zamiast czytał z nawiasów ('/mama/', '/tata/') żeby to czytał z pliku txt mam następujące pytanie co ma być w tym pliku txt oraz jak zmienić ten skrypt.

  1. <?php
  2. $text = ( $_POST['fname'] ) ;
  3. $patterns = array('/mama/', '/tata/');
  4. $replacements = array('<span class="unseen klasa0">{<span>mama</span>, <span class="x0">mamusia</span>, <span class="x0">mamka</span>}</span>', '<span class="unseen klasa0">{<span>tata</span>, <span class="x0">tatuś</span></span>}');
  5.  
  6. echo preg_replace ($patterns, $replacements, $text);
  7. ?>
  8.  
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 16)
hondek
post
Post #2





Grupa: Zarejestrowani
Postów: 355
Pomógł: 50
Dołączył: 20.08.2007
Skąd: Częstochowa

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


Hmm...
Plik przydało by się spreparoać tak, żeby kolejne patterny były oddzielone jakims separatorem, np ',' :
/mama/,/tata/

a co to skryptu to : fopen(), fread(), fclose(), explode().


--------------------
przetwornik yt na czasie :)
Go to the top of the page
+Quote Post
Puszkin4717
post
Post #3





Grupa: Zarejestrowani
Postów: 16
Pomógł: 0
Dołączył: 18.03.2007

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


a jak ten plik separować ? i nie wiem jak to wstawić bo pierwszy raz potrzebuje użyć tego żeby czytać coś z pliku.
Go to the top of the page
+Quote Post
hondek
post
Post #4





Grupa: Zarejestrowani
Postów: 355
Pomógł: 50
Dołączył: 20.08.2007
Skąd: Częstochowa

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


napisałem ci wszystkie funkcje jakie są ci potrzebne, manuala masz na http://php.net


--------------------
przetwornik yt na czasie :)
Go to the top of the page
+Quote Post
Puszkin4717
post
Post #5





Grupa: Zarejestrowani
Postów: 16
Pomógł: 0
Dołączył: 18.03.2007

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


a możesz dać jakiś przykład.
Go to the top of the page
+Quote Post
mmica
post
Post #6





Grupa: Zarejestrowani
Postów: 60
Pomógł: 20
Dołączył: 30.11.2010
Skąd: 127.0.0.1

Ostrzeżenie: (10%)
X----


Hey ;]
Po co ta niskopoziomowa zabawa z fopen() itd.. Myślałem, że lata fascynacji C minęły.

Plik patterns.txt:
Kod
/mama/,/tata/

Skrypt PHP:
  1. <?php
  2. $text = ($_POST['fname']);
  3.  
  4. $dane_z_pliku = file_get_contents('./patterns.txt');
  5. $patterns = explode(',', $dane_z_pliku);
  6.  
  7. $replacements = array('<span class="unseen klasa0">{<span>mama</span>, <span class="x0">mamusia</span>, <span class="x0">mamka</span>}</span>', '<span class="unseen klasa0">{<span>tata</span>, <span class="x0">tatuś</span></span>}');
  8.  
  9. echo preg_replace ($patterns, $replacements, $text);
  10. ?>

Pozdrawiam,
mmica


--------------------
"Imagination is more important than knowledge, because knowledge is limited." ~Albert Einstein
<?php $var = array('foo', 'bar'); ?>
Go to the top of the page
+Quote Post
Puszkin4717
post
Post #7





Grupa: Zarejestrowani
Postów: 16
Pomógł: 0
Dołączył: 18.03.2007

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


Nadal problem

Warning: Wrong parameter count for preg_replace() in /home/zwierz/public_html/gt1.php on line 12
Go to the top of the page
+Quote Post
mmica
post
Post #8





Grupa: Zarejestrowani
Postów: 60
Pomógł: 20
Dołączył: 30.11.2010
Skąd: 127.0.0.1

Ostrzeżenie: (10%)
X----


Hey ;]
W jaki sposób zmieniłeś mój kod? Ostrzeżenie wskazuje na linie 12., a takiej w moim kodzie nie ma winksmiley.jpg
U mnie taki kod spełnia zadanie:
  1. <?php
  2. $text = ($_GET['fname']);
  3.  
  4. $dane_z_pliku = file_get_contents('./patterns.txt');
  5. $patterns = explode(',', $dane_z_pliku);
  6.  
  7. $replacements = array('<span class="unseen klasa0">{<span>mama</span>, <span class="x0">mamusia</span>, <span class="x0">mamka</span>}</span>', '<span class="unseen klasa0">{<span>tata</span>, <span class="x0">tatuś</span></span>}');
  8.  
  9. echo preg_replace ($patterns, $replacements, $text);
  10. ?>
Odwołanie:
Kod
http://host/preg.php?fname=tata%20ma,%20a%20mama%20nie

A na wyjściu:
Kod
{tata, tatus} ma, a {mama, mamusia, mamka} nie


Pozdrawiam,
mmica


--------------------
"Imagination is more important than knowledge, because knowledge is limited." ~Albert Einstein
<?php $var = array('foo', 'bar'); ?>
Go to the top of the page
+Quote Post
Babcia@Stefa
post
Post #9





Grupa: Zarejestrowani
Postów: 654
Pomógł: 17
Dołączył: 19.03.2006
Skąd: z kosmosu ;)

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


Cytat(hondek @ 4.12.2010, 22:26:06 ) *
Hmm...
Plik przydało by się spreparoać tak, żeby kolejne patterny były oddzielone jakims separatorem, np ',' :
/mama/,/tata/

a co to skryptu to : fopen(), fread(), fclose(), explode().


file_get_contents() najprościej.

-- WebNuLL


--------------------
Środowisko testowe (desktop) - Gedit, lighttpd, sftp, rsync, xfce4-terminal, chromium, firefox4 | System: Gentoo ~x86
O'Neill - serwer WWW @ lighttpd, links, nano, rsyncd, sftpd | System: Debian
Go to the top of the page
+Quote Post
Puszkin4717
post
Post #10





Grupa: Zarejestrowani
Postów: 16
Pomógł: 0
Dołączył: 18.03.2007

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


zobacz http://hosting.jkns.pl/~zwierz/gt3.php

a błąd wyrzuca na tą linijkę

  1. echo preg_replace ($patterns, $replacements, $text);
Go to the top of the page
+Quote Post
Cysiaczek
post
Post #11





Grupa: Moderatorzy
Postów: 4 465
Pomógł: 137
Dołączył: 26.03.2004
Skąd: Gorzów Wlkp.




Zadałeś sobie trud przeczytania co to za błąd? Wpisz w google, to zobaczysz.


--------------------
To think for yourself you must question authority and
learn how to put yourself in a state of vulnerable, open-mindedness;
chaotic, confused, vulnerability, to inform yourself.
Think for yourself. Question authority.
Go to the top of the page
+Quote Post
mmica
post
Post #12





Grupa: Zarejestrowani
Postów: 60
Pomógł: 20
Dołączył: 30.11.2010
Skąd: 127.0.0.1

Ostrzeżenie: (10%)
X----


Hey ;]
  1. Warning: preg_replace() [function.preg-replace]: No ending delimiter '�' found in /home/zwierz/public_html/gt1.php on line 17

Tu chodzi o brak ogranicznika dla preg_replace(), tzn. w pliku patterns.txt miałeś:
Kod
/mama/,/tata/

Znak ' / ' jest właśnie tym ogranicznikiem i zdaje mi się, że gdzieś Ci go zabrakło. Jeżeli możesz, wklej cały kod - będzie łatwiej.

Pozdrawiam,
mmica


--------------------
"Imagination is more important than knowledge, because knowledge is limited." ~Albert Einstein
<?php $var = array('foo', 'bar'); ?>
Go to the top of the page
+Quote Post
Puszkin4717
post
Post #13





Grupa: Zarejestrowani
Postów: 16
Pomógł: 0
Dołączył: 18.03.2007

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


Plik TXT
  1. /mama/,/tata/



q1.php
  1. <?php
  2.  
  3. $text = ($_GET['fname']);
  4.  
  5.  
  6.  
  7. $dane_z_pliku = file_get_contents('./patterns.txt');
  8.  
  9. $patterns = explode(',', $dane_z_pliku);
  10.  
  11.  
  12.  
  13. $replacements = array('<span class="unseen klasa0">{<span>mama</span>, <span class="x0">mamusia</span>, <span class="x0">mamka</span>}</span>', '<span class="unseen klasa0">{<span>tata</span>, <span class="x0">tatuś</span></span>}');
  14.  
  15.  
  16.  
  17. echo preg_replace ($patterns, $replacements, $text);
  18.  
  19. ?>
Go to the top of the page
+Quote Post
mmica
post
Post #14





Grupa: Zarejestrowani
Postów: 60
Pomógł: 20
Dołączył: 30.11.2010
Skąd: 127.0.0.1

Ostrzeżenie: (10%)
X----


Hey ;]
Błąd masz w linii 3.:
  1. $text = ($_GET['fname']);
Korzystasz z GET'a, a w formularzu masz POST. Zamień na:
  1. $text = ($_POST['fname']);

Pozdrawiam,
mmica


--------------------
"Imagination is more important than knowledge, because knowledge is limited." ~Albert Einstein
<?php $var = array('foo', 'bar'); ?>
Go to the top of the page
+Quote Post
Puszkin4717
post
Post #15





Grupa: Zarejestrowani
Postów: 16
Pomógł: 0
Dołączył: 18.03.2007

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


Nadal błąd wywala
Go to the top of the page
+Quote Post
mmica
post
Post #16





Grupa: Zarejestrowani
Postów: 60
Pomógł: 20
Dołączył: 30.11.2010
Skąd: 127.0.0.1

Ostrzeżenie: (10%)
X----


Hey ;]
O rany.. http://my-pastebin.ugu.pl/helpdesk/preg.php - tu masz przykład, który działa. Pod przykładem wypisałem Ci kod na jakim to pracuje. Przekopiuj i sprawdź czy to działa.

Pozdrawiam,
mmica


--------------------
"Imagination is more important than knowledge, because knowledge is limited." ~Albert Einstein
<?php $var = array('foo', 'bar'); ?>
Go to the top of the page
+Quote Post
Puszkin4717
post
Post #17





Grupa: Zarejestrowani
Postów: 16
Pomógł: 0
Dołączył: 18.03.2007

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


Znalazłem błąd dzięki smile.gif

Ten post edytował Puszkin4717 5.12.2010, 14:26:33
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 20.08.2025 - 17:22