Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]Wyszukiwanie obrazka i modyfikowanie stringi
baracuda
post 9.05.2022, 20:57:39
Post #1





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 2.02.2022

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


Witam serdecznie,
Mam na stronie treści pobierane z bazy danych.

Załóżmy że mam zmienną $txt ze stringiem.

Zdarzają się w nich nich obrazki w różnych postaciach:
- <img alt="XXXX" class="img-fluid" src="random.name" style="height:930px; width:620px" />
- <img alt="XXXX" class="img-fluid" src="random.name"/>
- <img src="random.name"/>
etc

Potrzebuję dodać do każdego obrazka: class "picla" and data-label-class="label-class" - czyli w rezultacie:
- <img alt="XXXX" class="img-fluid picla" src="random.name" style="height:930px; width:620px" data-label-class="label-class"/>
- <img alt="XXXX" class="img-fluid picla" src="random.name" data-label-class="label-class"/>
- <img class="picla" src="random.name" data-label-class="label-class"/>


Wie ktoś może jak to zrobić?smile.gif
Go to the top of the page
+Quote Post
phpuser88
post 9.05.2022, 21:18:47
Post #2





Grupa: Zarejestrowani
Postów: 66
Pomógł: 1
Dołączył: 4.05.2019
Skąd: PHP5.6

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


  1. $txt = str_replace('img-fluid','img-fluid picla', $txt);
  2. $txt = str_replace('/>','data-label-class="label-class"/>', $txt);

Najprościej, niekoniecznie najlepiej. ( $txt .= w zależności w jaki sposób wyświetlasz)
Go to the top of the page
+Quote Post
trueblue
post 9.05.2022, 21:20:05
Post #3





Grupa: Zarejestrowani
Postów: 6 799
Pomógł: 1827
Dołączył: 11.03.2014

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


Zanim uruchomisz ten plugin, dodaj po prostu brakujące wartości i atrybuty w JS.
https://api.jquery.com/addclass/
https://api.jquery.com/attr/


--------------------
Go to the top of the page
+Quote Post
baracuda
post 9.05.2022, 21:38:41
Post #4





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 2.02.2022

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


Cytat(phpuser88 @ 9.05.2022, 22:18:47 ) *
  1. $txt = str_replace('img-fluid','img-fluid picla', $txt);
  2. $txt = str_replace('/>','data-label-class="label-class"/>', $txt);

Najprościej, niekoniecznie najlepiej. ( $txt .= w zależności w jaki sposób wyświetlasz)


to nie zadziała, bo w stringu mam też inne treści, tabelki itp... zdjęcia nie zawsze są :/

Cytat(trueblue @ 9.05.2022, 22:20:05 ) *
Zanim uruchomisz ten plugin, dodaj po prostu brakujące wartości i atrybuty w JS.
https://api.jquery.com/addclass/
https://api.jquery.com/attr/



to musi być po stronie php zmienione sad.gif
Go to the top of the page
+Quote Post
phpuser88
post 9.05.2022, 21:44:11
Post #5





Grupa: Zarejestrowani
Postów: 66
Pomógł: 1
Dołączył: 4.05.2019
Skąd: PHP5.6

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


Najlepiej byłoby odpytać SQL gdzie znajdują się wartości, które chcesz modyfikować, a następnie wykonać UPDATE i mieć problem z głowy.
edit:
Dużo gorszym rozwiązaniem byłoby zastosowanie if'a ze strpos(), a następnie $txt = str_replace('img-fluid','img-fluid picla" data-label-class="label-class" ', $txt);

Ten post edytował phpuser88 9.05.2022, 21:57:22
Go to the top of the page
+Quote Post
trueblue
post 10.05.2022, 07:20:54
Post #6





Grupa: Zarejestrowani
Postów: 6 799
Pomógł: 1827
Dołączył: 11.03.2014

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


Cytat(baracuda @ 9.05.2022, 22:38:41 ) *
to musi być po stronie php zmienione sad.gif

A możesz wytłumaczyć dlaczego w PHP?
Przecież biblioteka galerii jest w JS, przed jej uruchomieniem wystarczy dodać atrybuty i wartości również w JS. Co spowoduje, że skrypt galerii w trakcie inicjalizacji znajdziej to co potrzebuje i uruchomi się poprawnie.


Jeśli tak bardzo chcesz PHP:
https://kawalekkodu.pl/the-tag-is-out-there...domxpath-s01e01
https://kawalekkodu.pl/the-tag-is-out-there...domxpath-s01e02
https://kawalekkodu.pl/the-tag-is-out-there...domxpath-s01e03
https://kawalekkodu.pl/the-tag-is-out-there...domxpath-s01e04


--------------------
Go to the top of the page
+Quote Post
Salvation
post 10.05.2022, 07:48:39
Post #7





Grupa: Zarejestrowani
Postów: 396
Pomógł: 71
Dołączył: 15.07.2014

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


Cytat(trueblue @ 10.05.2022, 08:20:54 ) *
Przecież biblioteka galerii jest w JS

A gdzie jest taka informacja, że to o galerię chodzi? Bo przeczytałem temat i dopiero Ty wspominasz o galerii.

Ogólnie, to zrobiłbym to regexem i str_replace. Najpierw poszukaj czy zmienna $txt pasuje do patternu <img>, i później... Jeżeli ma już atrybut class, to dodaj klasę na końcu, jeżeli nie, to podmień 'img' na 'img class="..."' + jeżeli nie ma data-atrybutu, to podmień '>' na 'data-atrybut="...">'.

Ten post edytował Salvation 10.05.2022, 07:52:49
Go to the top of the page
+Quote Post
trueblue
post 10.05.2022, 08:13:31
Post #8





Grupa: Zarejestrowani
Postów: 6 799
Pomógł: 1827
Dołączył: 11.03.2014

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


Cytat(Salvation @ 10.05.2022, 08:48:39 ) *
A gdzie jest taka informacja, że to o galerię chodzi? Bo przeczytałem temat i dopiero Ty wspominasz o galerii.

A tutaj: http://arunmichaeldsouza.github.io/picla/


--------------------
Go to the top of the page
+Quote Post
baracuda
post 10.05.2022, 09:38:53
Post #9





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 2.02.2022

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


Cytat(trueblue @ 10.05.2022, 09:13:31 ) *


Tak, chodzi o PICLA smile.gif

zrobiłem taki kod:

  1. $('.content-view img').each(function() {
  2. if ($(this).hasClass('picla') === false) {
  3. $(this).addClass('picla');
  4. $(this).attr('data-label-class', 'label-class');
  5. console.log('- dodaje do' + $(this).attr('src'));
  6. }
  7. });



ale nie dodaje napisów na zdjęciu (jak dodam ręcznie z poziomu kodu html to działa).

JS byłļy lepszy, ale nie chce działać sad.gif Chyba że robi się to jakoś inaczej?
Go to the top of the page
+Quote Post
trueblue
post 10.05.2022, 09:48:13
Post #10





Grupa: Zarejestrowani
Postów: 6 799
Pomógł: 1827
Dołączył: 11.03.2014

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


Z tego co widzę, to podpisy pobierane są z atrybutu alt.
Jeśli brak tego atrybutu, to podpisu nie będzie.

Ten post edytował trueblue 10.05.2022, 09:49:48


--------------------
Go to the top of the page
+Quote Post
baracuda
post 10.05.2022, 09:53:05
Post #11





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 2.02.2022

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


Zdjęcie ma taki atrybut:
  1. <p style="text-align:justify">
  2. <img alt="ornitolog koordynujący montaż budek" src="foto.jpg" style="height:930px; width:620px"/>
  3. </p>


Po dodaniu JSa wyglądada:

https://ibb.co/k05fwBN
Go to the top of the page
+Quote Post
trueblue
post 10.05.2022, 10:14:42
Post #12





Grupa: Zarejestrowani
Postów: 6 799
Pomógł: 1827
Dołączył: 11.03.2014

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


W odpowiedniej kolejności uruchamiasz skrypty, tj. najpierw Twój, potem picla?


--------------------
Go to the top of the page
+Quote Post
baracuda
post 10.05.2022, 10:40:27
Post #13





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 2.02.2022

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


Problem rozwiązany smile.gif Miałem złą kolejność smile.gif
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: 12.06.2025 - 20:23