Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [HTML][PHP]Wyciągnięcie atrybutu z taga i wstawienie go do innego
Laran
post 22.04.2014, 10:09:13
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
rocktech.pl
post 22.04.2014, 10:38:37
Post #2





Grupa: Zarejestrowani
Postów: 587
Pomógł: 131
Dołączył: 8.02.2010

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


Witam.


Zacznij od tego ...

  1. $HTMLs = '<a class="image" href="http://example.com/index.php?title=File:example.jpg">
  2. <img width="48" height="48" src="http://example.com/images/9/90/example.jpg" alt="Bombing.jpg"></img>
  3. </a>';
  4.  
  5. libxml_use_internal_errors(true);
  6. $html = new DOMDocument();
  7. $html->validateOnParse = false;
  8. $html->loadHTML($HTMLs);
  9.  
  10. foreach ( $html->getElementsByTagName('a') as $a) {
  11. echo $a->getAttribute('href');
  12. }


Ten post edytował rocktech.pl 22.04.2014, 10:40:28


--------------------
Despite the tons of examples and docs, mod_rewrite is voodoo. Damned cool voodoo, but still voodoo. --Brian Moore

I never go looking for a sucker. I look for a Champion and make a sucker of of him. --Amarillo Slim


Home-made : js-gui-classes | Accordion | Tabs | Carousel / php-sms-classes | Obsługa bramki SMS MultiInfo | Obsługa bramki SMS Mobiltek
Go to the top of the page
+Quote Post
Laran
post 22.04.2014, 11:21:06
Post #3





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

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.04.2024 - 14:06