Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> zamiana stringa na array
Randallmaster
post 23.03.2017, 15:01:26
Post #1





Grupa: Zarejestrowani
Postów: 677
Pomógł: 11
Dołączył: 18.11.2009

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


W jaki sposób poniższy string można zamienić na tablicę?

width: 500px; height: 500px; border-width: 5px; border-radius: 30px;

[width] => 500
[height] => 500
[border-width] => 5
itd.
questionmark.gif?
Go to the top of the page
+Quote Post
nospor
post 23.03.2017, 15:05:15
Post #2





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




explode po sredniku
a potem kazdy element
explode po dwukropku


Mozna tez uzyc wyrazen regularnych, jak ktos je kojarzy


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
kpt_lucek
post 23.03.2017, 15:38:59
Post #3





Grupa: Zarejestrowani
Postów: 428
Pomógł: 77
Dołączył: 10.07.2011
Skąd: Warszawa

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


  1. // Tak na szybko
  2.  
  3. <?php
  4. $re = '/([a-z\-]+)\:\s([0-9]+px)/';
  5. $str = 'width: 500px; height: 500px; border-width: 5px; border-radius: 30px;';
  6.  
  7. preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
  8.  
  9. $data = [];
  10. array_walk($matches, function ($item) use (&$data) {
  11. $data[$item[1]] = $item[2];
  12. });
  13.  
  14. print_r($data);


Ten post edytował kpt_lucek 23.03.2017, 15:39:10


--------------------


Cytat
There is a Bundle for that
Lukas Kahwe Smith - October 31th, 2014
Go to the top of the page
+Quote Post
Pyton_000
post 23.03.2017, 17:41:07
Post #4





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

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


Dużo wad ma ten regexp.

Proszę banalna metoda zaczerpnięta z Stacka:
  1. function BreakCSS($CSS) {
  2. $results = array();
  3.  
  4. foreach(explode(';', $CSS) AS $attr)
  5. if (strlen(trim($attr)) > 0) // for missing semicolon on last element, which is legal
  6. {
  7. list($name, $value) = explode(':', $attr);
  8. $results[trim($name)] = trim($value);
  9. }
  10. return $results;
  11. }
Go to the top of the page
+Quote Post
trueblue
post 23.03.2017, 18:11:54
Post #5





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

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


  1. $styles = "width: 500px; height: 500px; border-width: 5px; border-radius: 30px; background-image:url(\"aaa\")";
  2. $styles=str_replace(",\" \"","", "{\"" . strtr($styles, array(";"=>"\",\"",":"=>"\":\"","\""=>"'")) . "\"}");
  3. $json = json_decode($styles, true);
  4. $keys = array_map('trim', array_keys($json));
  5. $data = array_map('trim', $json);
  6. $result = array_combine($keys, $data);
  7. print_r($result);


//poprawione.

Ten post edytował trueblue 23.03.2017, 18:47:37


--------------------
Go to the top of the page
+Quote Post
Pyton_000
post 23.03.2017, 18:31:57
Post #6





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

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


; na końcu wysypuje skrypt, więc trzeba zadbać o to żeby go tam nie było.
Go to the top of the page
+Quote Post
trzczy
post 25.03.2017, 15:53:31
Post #7





Grupa: Zarejestrowani
Postów: 460
Pomógł: 49
Dołączył: 5.06.2011

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


array_walk II smile.gif

  1. <?php
  2. $str = 'width: 500px; height: 500px; border-width: 5px; border-radius: 30px;';
  3. $array2 = preg_split('/([\s;:]+)/', $str);
  4. $array2, function ($el) use (&$array, &$key) {
  5. if ($key) {
  6. $array[$key] = $el;
  7. $key = null;
  8. } else $key = $el;
  9. }
  10. );
  11. print_r($array);
Go to the top of the page
+Quote Post
trueblue
post 25.03.2017, 16:04:14
Post #8





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

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


Wszystkie powyższe metody wykładają się, jeśli pojawi się: background-image:url(http://domena.pl/aaa.jpg)


--------------------
Go to the top of the page
+Quote Post
viking
post 25.03.2017, 16:24:23
Post #9





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

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


https://github.com/sabberworm/PHP-CSS-Parser


--------------------
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: 25.06.2025 - 16:40