Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [HTML][PHP]Regexp - jak wyciaganc tylko tekst
dolar
post
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
viking
post
Post #2





Grupa: Zarejestrowani
Postów: 6 381
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
bostaf
post
Post #3





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

Posty w temacie


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: 29.09.2025 - 19:05