Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]preg_match pomoc w napisaniu patternu
newBd
post 25.07.2016, 18:19:45
Post #1





Grupa: Zarejestrowani
Postów: 10
Pomógł: 0
Dołączył: 28.06.2016

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


Witam
Chciałbym prosić o pomoc w napisaniu wyrazenia regularnego, ktore bedzie wyciagac linki z tekstu.

Przykladowy tekst zrodlowy
Kod
<li><a href="link1.html"> Link 1</a></li>
<li><a href="link2.html"> Link 2</a></li>
<li><a href="link3.html"> Link 3</a></li>
<li><a href="link4.html"> Link 4</a></li>
<li><a href="link5.html"> Link 5</a></li>


i teraz chcialbym, aby funkcja preg_match wyciagnela mi tylko to, co jest wewnatrz href (link1.html, link2.html...itd)

Prosze o pomoc.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 4)
daro0
post 25.07.2016, 19:07:02
Post #2





Grupa: Zarejestrowani
Postów: 88
Pomógł: 12
Dołączył: 17.09.2014
Skąd: Krasnystaw

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


A np. tak:

  1. $source='<li><a href="link1.html"> Link 1</a></li>';
  2. preg_match("'<li><a href=\"link1.html\">(.*?)<\/a><\/li>'si", $source, $match);
  3. if($match) echo $match[1];


i po kolei musisz jechać z kolejnymi linkami wstawiając to jako pattern czyli

  1. $source='<li><a href="link2.html"> Link 2</a></li>';
  2. preg_match("'<li><a href=\"link2.html\">(.*?)<\/a><\/li>'si", $source, $match);
  3. if($match) echo $match[1];


  1. $source='<li><a href="link3.html"> Link 3</a></li>';
  2. preg_match("'<li><a href=\"link3.html\">(.*?)<\/a><\/li>'si", $source, $match);
  3. if($match) echo $match[1];


itd.

http://stackoverflow.com/questions/1586779...tween-html-tags

to też działa:
  1. $source='<li><a href="link3.html"> Link 3</a></li>';
  2. preg_match('/<li><a href="link3.html">(.*?)<\/a><\/li>/si', $source, $match);
  3. if($match) echo $match[1];


Ten post edytował daro0 25.07.2016, 19:14:22
Go to the top of the page
+Quote Post
newBd
post 25.07.2016, 19:13:12
Post #3





Grupa: Zarejestrowani
Postów: 10
Pomógł: 0
Dołączył: 28.06.2016

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


A jak zrobić, jeśli nie znam linkow wewnatrz href?
Druga sprawa, w jakiej petli to umiescic, zeby wyswietlic wszystkie wystapienia?
Go to the top of the page
+Quote Post
trueblue
post 25.07.2016, 19:23:10
Post #4





Grupa: Zarejestrowani
Postów: 6 806
Pomógł: 1828
Dołączył: 11.03.2014

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


http://stackoverflow.com/questions/3820666...of-an-a-element


--------------------
Go to the top of the page
+Quote Post
mlawnik
post 26.07.2016, 14:46:14
Post #5





Grupa: Zarejestrowani
Postów: 455
Pomógł: 49
Dołączył: 12.04.2010

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


Dokładnie, parsowanie HTML regexem to porażka.


--------------------
Pozdrawiam,
mlawnik

Cytat(nospor)
Jedzmy gówno.... miliony much nie może się przecież mylić
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: 24.07.2025 - 17:22