Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [HTML][PHP]Wyciągnięcie atrybutu z taga i wstawienie go do innego
Laran
post
Post #1





Grupa: Zarejestrowani
Postów: 25
Pomógł: 1
Dołączył: 20.03.2013

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


Witam.

Chciałbym zamienić wartość atrybutu "href" tagu "a" na wartość atrybutu "src" tagu "img". Przykład obecnego kodu:
  1. <a class="image" href="http://example.com/index.php?title=File:example.jpg">
  2.  
  3. <img width="48" height="48" src="http://example.com/images/9/90/example.jpg" alt="Bombing.jpg"></img>
  4.  
  5. </a>

Rezultat jaki chce osiągnąć to:
  1. <a class="image" href="http://example.com/images/9/90/example.jpg">
  2.  
  3. <img width="48" height="48" src="http://example.com/images/9/90/example.jpg" alt="Bombing.jpg"></img>
  4.  
  5. </a>

Proszę o pomoc, bo nie mam pojęcia jak to ugryźć.

Pozdrawiam.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Laran
post
Post #2





Grupa: Zarejestrowani
Postów: 25
Pomógł: 1
Dołączył: 20.03.2013

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


Wielkie dzięki za pomoc. Rezultat końcowy:

  1. function imageHref($content, $class)
  2. {
  3. libxml_use_internal_errors(true);
  4. $html = new DOMDocument();
  5. $html->validateOnParse = false;
  6. $html->loadHTML($content);
  7. $finder = new DomXPath($html);
  8. $nodes = $finder->query('//a[@class="'.$class.'"]');
  9.  
  10. foreach ($nodes as $a)
  11. {
  12. foreach ($a->getElementsByTagName('img') as $a2)
  13. {
  14. $new = $a2->getAttribute('src');
  15.  
  16. $a->removeAttribute('href');
  17. $a->setAttribute('href', $new);
  18. }
  19. }
  20.  
  21. $content = $html->saveHTML();
  22. return $content;
  23. }


Temat do zamknięcia.
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: 18.10.2025 - 01:12