Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] wyciągnięcie danych z pierwszego znacznika SPAN
--zenek--
post 11.09.2012, 12:02:24
Post #1





Goście







Witam

Mam taki html:
  1. <div><span>aaa</span> <span>bbb</span> <span>ccc</span></div>
  2. <div><span>ddd</span> <span>eee</span> <span>fff</span></div>
  3. <div><span>ggg</span> <span>hhh</span> <span>iii</span></div>

mogę prosić o podpowiedź jakiego wyrażenia regularnego w kombinacji z preg_match_all użyć żeby wyciągnąć dane tylko z pierwszego znacznika span w każdym divie?

zenek
Go to the top of the page
+Quote Post
kamil4u
post 11.09.2012, 12:23:03
Post #2





Grupa: Zarejestrowani
Postów: 2 350
Pomógł: 512
Dołączył: 4.01.2009
Skąd: Wrocław / Świdnica

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


Trzeba zmienić zachłanność wyrażenia
  1. $test = '<div><span>aaa</span> <span>bbb</span> <span>ccc</span></div>
  2. <div><span>ddd</span> <span>eee</span> <span>fff</span></div>
  3. <div><span>ggg</span> <span>hhh</span> <span>iii</span></div>';
  4.  
  5. preg_match_all('#<div><span>(.*?)\</span>#',$test, $matches);
  6.  
  7. print_r($matches);


--------------------
Go to the top of the page
+Quote Post
--zenek--
post 11.09.2012, 12:33:44
Post #3





Goście







Cytat(kamil4u @ 11.09.2012, 12:23:03 ) *
Trzeba zmienić zachłanność wyrażenia
  1. $test = '<div><span>aaa</span> <span>bbb</span> <span>ccc</span></div>
  2. <div><span>ddd</span> <span>eee</span> <span>fff</span></div>
  3. <div><span>ggg</span> <span>hhh</span> <span>iii</span></div>';
  4.  
  5. preg_match_all('#<div><span>(.*?)\</span>#',$test, $matches);
  6.  
  7. print_r($matches);



Dziękuje, a jakbym chciał nie pierwszy tylko nty, np 2 span z diva?
Go to the top of the page
+Quote Post
viking
post 11.09.2012, 12:42:46
Post #4





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

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


Poczytaj o XPath i http://php.net/manual/en/book.dom.php a nie regexpy do obsługi DOM.


--------------------
Go to the top of the page
+Quote Post
kamil4u
post 11.09.2012, 12:44:33
Post #5





Grupa: Zarejestrowani
Postów: 2 350
Pomógł: 512
Dołączył: 4.01.2009
Skąd: Wrocław / Świdnica

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


Jak masz takie dane jakie masz to najłatwiej:
  1. $test = '<div><span>aaa</span> <span>bbb</span> <span>ccc</span></div>
  2. <div><span>ddd</span> <span>eee</span> <span>fff</span></div>
  3. <div><span>ggg</span> <span>hhh</span> <span>iii</span></div>';
  4.  
  5. $test = explode('<span>',$test);
  6. print_r($test);


A wyrażeniami to:
  1. $test = '<div><span>aaa</span> <span>bbb</span> <span>ccc</span></div>
  2. <div><span>ddd</span> <span>eee</span> <span>fff</span></div>
  3. <div><span>ggg</span> <span>hhh</span> <span>iii</span></div>';
  4.  
  5. preg_match_all('#<span>(.*?)\</span>#',$test, $matches);
  6.  
  7. print_r($matches);


I pętlą co np. trzeci element przejechać.

I wyrażaniami tylko drugie elementy:
  1. $test = '<div><span>aaa</span> <span>bbb</span> <span>ccc</span></div>
  2. <div><span>ddd</span> <span>eee</span> <span>fff</span></div>
  3. <div><span>ggg</span> <span>hhh</span> <span>iii</span></div>';
  4.  
  5. preg_match_all('#<div>(<span>.*?){2}(.*?)\</span>#',$test, $matches);
  6.  
  7. print_r($matches);


--------------------
Go to the top of the page
+Quote Post
wNogachSpisz
post 11.09.2012, 12:56:52
Post #6





Grupa: Zarejestrowani
Postów: 1 233
Pomógł: 87
Dołączył: 6.03.2009

Ostrzeżenie: (40%)
XX---


[JAVASCRIPT] pobierz, plaintext
  1. var span = document.querySelector('div > span:nth-child(1)')
[JAVASCRIPT] pobierz, plaintext


Ten post edytował wNogachSpisz 11.09.2012, 12:57:18
Go to the top of the page
+Quote Post
kamil4u
post 11.09.2012, 13:00:46
Post #7





Grupa: Zarejestrowani
Postów: 2 350
Pomógł: 512
Dołączył: 4.01.2009
Skąd: Wrocław / Świdnica

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


@wNogachSpisz: Wszystko fajnie, ale:
- to jest JS, a mówimy o PHP
- nie zadziała to pod starszymi przeglądarkami

Ten post edytował kamil4u 11.09.2012, 13:01:00


--------------------
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: 26.06.2025 - 18:26