Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]preg_match - wydobycie wartości z div'a
Majkelo23
post
Post #1





Grupa: Zarejestrowani
Postów: 226
Pomógł: 17
Dołączył: 13.02.2012

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


Mam kilkanaście takich divów:

  1. id="quest_3"
  2. data-id="3"
  3. data-progress="1"
  4. data-goals="1"
  5. data-url="/game.php"
  6. class="quest opened finished "
  7. title="Godziwa nazwa"
  8. style="background-image:url( 'http://dspl.pl' );">


Potrzebuję wyciągnąć z nich liczbę: data-id="3" ale tylko w przypadku, gdy klasa jest równa: quest opened finished bo pozostałe divy mają klasę: quest opened (bez finished).

Napisałem takie coś:

  1. preg_match_all('"<div id=\"quest_([0-9]+)\" data-id=\"([0-9]+)\" data-progress=\"([0-9]+)\" data-goals=\"([0-9]+)\" data-url=\"(.*?)\" class=\"quest opened finished \""x', $v, $matching_questions);


Ale to nic nie znajduje.

Ten post edytował Majkelo23 31.08.2014, 12:13:31
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 5)
SmokAnalog
post
Post #2





Grupa: Zarejestrowani
Postów: 1 707
Pomógł: 266
Dołączył: 3.07.2012
Skąd: Poznań

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


Użyj PHP DOM, albo jakiejś wygodniejszej biblioteki, np. phpQuery.
Go to the top of the page
+Quote Post
Majkelo23
post
Post #3





Grupa: Zarejestrowani
Postów: 226
Pomógł: 17
Dołączył: 13.02.2012

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


No dobra, a jeśli jednak wolałbym pozostać przy preg_matchu?
Go to the top of the page
+Quote Post
Pyton_000
post
Post #4





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

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


Kod
/<div\s*id="quest_([0-9]+)"\s*data-id="([0-9]+)"\s*data-progress="([0-9]+)"\s*data-goals="([0-9]+)"\s*data-url="(.*?)"\s*class="quest opened finished "/
Go to the top of the page
+Quote Post
Majkelo23
post
Post #5





Grupa: Zarejestrowani
Postów: 226
Pomógł: 17
Dołączył: 13.02.2012

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


Poradziłem sobie:
  1. preg_match_all('/<div(?:\s|\S)*?data\-id=\"([\d]+)\"(?:\s|\S)*?class=\"\s*quest opened finished\s*\"/i', $game_value, $matching_questions);
Go to the top of the page
+Quote Post
SmokAnalog
post
Post #6





Grupa: Zarejestrowani
Postów: 1 707
Pomógł: 266
Dołączył: 3.07.2012
Skąd: Poznań

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


Fajnie, ale ten kod jest paskudny.
Go to the top of the page
+Quote Post

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: 26.09.2025 - 04:06