Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [HTML][PHP]Regexp - jak wyciaganc tylko tekst
dolar
post 24.05.2017, 10:06:19
Post #1





Grupa: Zarejestrowani
Postów: 92
Pomógł: 0
Dołączył: 19.10.2016

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


MAm taki oto zapis z html:

  1. <div class="latestItemIntroText">
  2.  
  3. <div class="itemLinks">
  4. <div class="share">Share</div>
  5. <div class="dummy-div"></div>
  6.  
  7. <div class="addthis_sharing_toolbox"></div>
  8.  
  9.  
  10. </div> (</p> ---> to moze byc lub nie)
  11.  
  12. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
  13. (<br /><br />) -> tutaj tez czasem moze byc lub nie jakas br lub cokolwiek
  14. ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
  15.  
  16. </p><br/> --> to tez moze sie pojawic lub nie </div>


Jak do tej pory mam taki zapis:

<div class="addthis_sharing_toolbox"><\/div>([ \s?]+)<\/div>([^<]+)<\/div>

To mi ladnie wyciaga tekst, ale bez <p>, bo jesli znajdzie sie tam pomiedzy <p> lub <br/> to juz jest problem i mi nie wyciaga textu. Jak mozna ulepszyc moj zapis regex? O ile da sie cos takiego w ogole wyciaganc...

Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 11)
viking
post 24.05.2017, 10:10:32
Post #2





Grupa: Zarejestrowani
Postów: 6 378
Pomógł: 1116
Dołączył: 30.08.2006

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


Wyrażenia regularne nie służą do obróbki DOM. Zainteresuj się funkcjami DOM typu simplexml.


--------------------
Go to the top of the page
+Quote Post
dolar
post 24.05.2017, 10:13:37
Post #3





Grupa: Zarejestrowani
Postów: 92
Pomógł: 0
Dołączył: 19.10.2016

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


Hmm nigdy o tym co mi napsiales jeszcze nie slyszalam i pewnie bede meic sporo pracy nad tym... no ale coz jak sie nie da regexp to poczytam o tych DOM cokolwiek to jest ;p tylko ze moj plik to htlm jest...

Ten post edytował dolar 24.05.2017, 10:15:03
Go to the top of the page
+Quote Post
bostaf
post 24.05.2017, 11:05:59
Post #4





Grupa: Zarejestrowani
Postów: 374
Pomógł: 79
Dołączył: 6.04.2010
Skąd: Ostrów Wielkopolski

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


Cytat(viking @ 24.05.2017, 11:10:32 ) *
Wyrażenia regularne nie służą do obróbki DOM. Zainteresuj się funkcjami DOM typu simplexml.

Ja się po tym podpisuję, i zasugerowałbym też hasło "html/dom parser", ale dla zabawy zrobiłem tego regexa:
Kod
<div class="addthis_sharing_toolbox"><\/div>\s+<\/div>((^|.|\s)+)<\/div>

sam tekst czyli to co w regexie w nawiasie wyciągniesz w php preg_match (parametr matches), a te zbędne tagi wewnątrz tekstu strip_tags. Samym regexem nie da się pominąć fragmentów tekstu.
Go to the top of the page
+Quote Post
dolar
post 24.05.2017, 13:13:46
Post #5





Grupa: Zarejestrowani
Postów: 92
Pomógł: 0
Dołączył: 19.10.2016

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


@bostav @vikinh - dzieki za podpowiedz. Wlasnie czytam o tych DOM'ach. Juz na samym starcie mam problem, gdyz jak probuje uzyc funkcji :
  1. $html = file_get_html($link);


To mam error :

l error: Uncaught Error: Call to undefined function file_get_html() in /one.php:243 Stack trace: #0 {main} thrown in /one.php on line 243

Czy to wina mojego edytora ze nie zna tej funkcji?
Go to the top of the page
+Quote Post
viking
post 24.05.2017, 13:17:41
Post #6





Grupa: Zarejestrowani
Postów: 6 378
Pomógł: 1116
Dołączył: 30.08.2006

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


Wypadało by wcześniej dołączyć pliki biblioteki simple html.


--------------------
Go to the top of the page
+Quote Post
dolar
post 24.05.2017, 13:19:38
Post #7





Grupa: Zarejestrowani
Postów: 92
Pomógł: 0
Dołączył: 19.10.2016

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


Cytat(viking @ 24.05.2017, 14:17:41 ) *
Wypadało by wcześniej dołączyć pliki biblioteki simple html.


Fakt, wlasnie sciagnelam te pliki... hmm a jak je powinnam dlaczyc? teraz mam ten plik w Pobranych dokumentach. Mam go przeniesc do swojego projektu rozuemim?

Dobra dalam rade... jade dalej z kodem

No dobra chce wyciaganc ten zakichany tekst stad:

  1. <div class="latestItemIntroText">
  2.  
  3. <div class="itemLinks">
  4. <div class="share">Podziel się</div>
  5. <div class="dummy-div"></div>
  6.  
  7. <div class="addthis_sharing_toolbox"></div>
  8.  
  9. </div>
  10. Mamy dobre informacje dla stojących w dużym korku w okolicach miejscowości Nowak. Ruch jest już odblokowany, powoli sytuacja powinna się polepszać. <br /><br />Jak informują nas drogowcy, ruch zostanie jeszcze raz chwilowo wstrzymany, ale tylko na czas załadowania samochodów na lawetę.
  11.  
  12. </div>
  13.  
  14. <div class="clr"></div>
  15.  


Tak sobie ktos zaprojektowal strone i stad musze wyciaganc. Probuje tak:

$info['opis']= $html->find('.addthis_sharing_toolbox div');

Ale zwraca pusta tablice.

Ten post edytował dolar 24.05.2017, 13:35:29
Go to the top of the page
+Quote Post
Pyton_000
post 24.05.2017, 13:41:47
Post #8





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


Kod
<div class="addthis_sharing_toolbox"></div>

Ciekawe dla czego...
Lepiej poszukaj `.latestItemIntroText`
Go to the top of the page
+Quote Post
dolar
post 24.05.2017, 13:53:20
Post #9





Grupa: Zarejestrowani
Postów: 92
Pomógł: 0
Dołączył: 19.10.2016

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


$html->find('.latestItemIntroText')->innertext;

Dalej null
Go to the top of the page
+Quote Post
Pyton_000
post 24.05.2017, 14:02:52
Post #10





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


var_dump($html->find('.latestItemIntroText'));
Go to the top of the page
+Quote Post
dolar
post 24.05.2017, 14:07:46
Post #11





Grupa: Zarejestrowani
Postów: 92
Pomógł: 0
Dołączył: 19.10.2016

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


No dobra, a czy jest mozliwosc tym DOM'em zrobic wyjatek z tej mojej klasy i nie wyciagac tekstu "Share"? Bo tez mi go pobiera jako text tegoz diva

Ktokolwiek? sad.gif

Ten post edytował dolar 24.05.2017, 14:37:26
Go to the top of the page
+Quote Post
viking
post 24.05.2017, 15:24:09
Post #12





Grupa: Zarejestrowani
Postów: 6 378
Pomógł: 1116
Dołączył: 30.08.2006

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


simplehtml nie obsługuje selektora :not.
Możesz usunąć po prostu ten tekst albo np za pomocą biblioteki phpquery:
  1. $xml = phpQuery::newDocumentHTML($html);
  2. pq('.itemLinks')->remove();
  3. $x = pq('.latestItemIntroText');
  4. echo $x[0]->text();


--------------------
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: 19.07.2025 - 06:50