Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Preg match diva
Watt
post 2.04.2010, 13:49:17
Post #1





Grupa: Zarejestrowani
Postów: 372
Pomógł: 1
Dołączył: 13.06.2008

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


Witam,

Mam diva i chciałbym z niego wyciągnąć treść:

Kod
<div style="" dir="ltr"> costam </div>

więc napisałem kod
  1. preg_match_all("/<div style=\"\" dir=\"ltr\"[^>]*>(.*?)<\/div>/",$result ,$matches);


Lecz on nie chce działać - co zrobiłem źle?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 11)
PiotrekM
post 2.04.2010, 14:06:37
Post #2





Grupa: Zarejestrowani
Postów: 130
Pomógł: 6
Dołączył: 20.12.2009

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


  1. <?php
  2. $result = '<div style="" dir="ltr"> costam </div>';
  3. preg_match_all('#<div style="" dir="ltr">(.*?)</div>#si', $result ,$matches);
  4. print_r($matches);


Ten post edytował PiotrekM 2.04.2010, 14:11:33
Go to the top of the page
+Quote Post
Pawel_W
post 2.04.2010, 14:06:49
Post #3





Grupa: Zarejestrowani
Postów: 1 675
Pomógł: 286
Dołączył: 15.06.2009
Skąd: Wieliczka

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


po 1: zmień ograniczniki, zamiast / używaj #, nie będziesz musiał / escape'ować smile.gif (to tylko taka uwaga)
po 2: zamiast " używaj ', również nie będziesz musiał escape'ować
po 3: co to jest? to jest chyba w tym przypadku niepotrzebne smile.gif
Cytat
[^>]*


stosując się do powyższych kod powinien wyglądać tak:
  1. <?php
  2. $result = '<div style="" dir="ltr"> costam </div>';
  3. preg_match_all('#<div style="" dir="ltr">(.*?)</div>#',$result ,$matches);
  4. print_r($matches);
  5. ?>
Go to the top of the page
+Quote Post
Watt
post 2.04.2010, 15:21:32
Post #4





Grupa: Zarejestrowani
Postów: 372
Pomógł: 1
Dołączył: 13.06.2008

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


Właśnie jest problem:

Kod
Array ( [0] => Array ( ) [1] => Array ( ) )

To mi zwraca.

Cały kod
  1. $link = "http://translate.google.com/";
  2. $polaczenie = @curl_init();
  3. @curl_setopt($polaczenie, CURLOPT_URL, $link);
  4. @curl_setopt($polaczenie, CURLOPT_POSTFIELDS, 'text='.urlencode($contents).'&sl=pl&tl=en'); //dane do wyslania
  5. @curl_setopt($polaczenie, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)");
  6. @curl_setopt($polaczenie, CURLOPT_FOLLOWLOCATION, 1);
  7. @curl_setopt($polaczenie, CURLOPT_RETURNTRANSFER, 1);
  8. $result = @curl_exec ($polaczenie);
  9. echo $result;
  10. preg_match_all('#<div style="" dir="ltr">(.*?)</div>#',$result ,$matches);
  11. print_r($matches);
  12. $result = $matches[1][0]."</span>";
  13. preg_match_all("/<span[^>]*>(.*?)<\/span>/",$result ,$matches);
  14. $contents = $matches[1][0];


na dole tej strone
http://pgc-cs.eu/stare/
Go to the top of the page
+Quote Post
Pawel_W
post 2.04.2010, 15:29:56
Post #5





Grupa: Zarejestrowani
Postów: 1 675
Pomógł: 286
Dołączył: 15.06.2009
Skąd: Wieliczka

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


to może powiesz co chcesz wyciągnąć? bo w kodzie tej frazy nie widzę...
Go to the top of the page
+Quote Post
Watt
post 2.04.2010, 18:13:08
Post #6





Grupa: Zarejestrowani
Postów: 372
Pomógł: 1
Dołączył: 13.06.2008

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


Chce wyciagnąć to co 'wypluł' translator google, a to znajduje się w tym divie, jak nie mylę.

Ten post edytował Watt 2.04.2010, 18:13:18
Go to the top of the page
+Quote Post
Pawel_W
post 2.04.2010, 18:49:43
Post #7





Grupa: Zarejestrowani
Postów: 1 675
Pomógł: 286
Dołączył: 15.06.2009
Skąd: Wieliczka

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


no to było tak od razu... smile.gif
  1. preg_match('#<input type=hidden name=gtrans value="(.*)"><div id=sug_exp>#',$result,$matches);
  2. echo $matches[1];

o to chodziło?
Go to the top of the page
+Quote Post
Watt
post 2.04.2010, 18:51:37
Post #8





Grupa: Zarejestrowani
Postów: 372
Pomógł: 1
Dołączył: 13.06.2008

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


To jest kod który ja wpisuje, ja chce pobrać kod który jest pod napisem
"Polish to English translation"

Ten post edytował Watt 2.04.2010, 18:53:56
Go to the top of the page
+Quote Post
Pawel_W
post 2.04.2010, 18:54:00
Post #9





Grupa: Zarejestrowani
Postów: 1 675
Pomógł: 286
Dołączył: 15.06.2009
Skąd: Wieliczka

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


skopiowałem Twój skrypt, dodałem to co wyżej, i mi jakoś tłumaczy...
Go to the top of the page
+Quote Post
Watt
post 2.04.2010, 18:58:56
Post #10





Grupa: Zarejestrowani
Postów: 372
Pomógł: 1
Dołączył: 13.06.2008

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


Tak, on tłumaczy z tym, że ja nie chce pobierać całej strony google tylko kod, który bedzie przetłumaczony.
Go to the top of the page
+Quote Post
Pawel_W
post 2.04.2010, 19:02:22
Post #11





Grupa: Zarejestrowani
Postów: 1 675
Pomógł: 286
Dołączył: 15.06.2009
Skąd: Wieliczka

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


no a co ja napisałem...
ehh, dobra, łap mój kod:
  1. <?php
  2. $contents = "PHP - obiektowy, skryptowy język programowania zaprojektowany do generowania stron internetowych w czasie rzeczywistym."; // z wikipedii ;o
  3. $link = "http://translate.google.com/";
  4. $polaczenie = @curl_init();
  5. @curl_setopt($polaczenie, CURLOPT_URL, $link);
  6. @curl_setopt($polaczenie, CURLOPT_POSTFIELDS, 'text='.urlencode($contents).'&sl=pl&tl=en'); //dane do wyslania
  7. @curl_setopt($polaczenie, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)");
  8. @curl_setopt($polaczenie, CURLOPT_FOLLOWLOCATION, 1);
  9. @curl_setopt($polaczenie, CURLOPT_RETURNTRANSFER, 1);
  10. $result = @curl_exec ($polaczenie);
  11. preg_match('#<input type=hidden name=gtrans value="(.*)"><div id=sug_exp>#',$result,$matches);
  12. echo $matches[1];
  13. ?>
Go to the top of the page
+Quote Post
Watt
post 2.04.2010, 19:31:33
Post #12





Grupa: Zarejestrowani
Postów: 372
Pomógł: 1
Dołączył: 13.06.2008

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


Oj, wybacz zobaczyłem input - sprawdzę czy działa.
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 Wersja Lo-Fi Aktualny czas: 18.07.2025 - 01:44