I sparsed information from the site and I have about 1000 div -like

 <div style='background-color:#2E2E2E;' botNum='1' id='5' inspectLink='' valueType='Key' amount='52' original-name='Chroma 2 Case Key' title='<div style=\"margin:-8px -8px; padding:3px 10px; padding-bottom:5px; color:#57a857;font-size:16px;background:rgba(26,26,26, 1);\">Chroma 2 Case Key<\/div>' classId='927007517' value='2.85' class="itemImgDiv" clickable='true'> <div class='propsWrapper'> <span class='number'> <b id='num5'>x52</b> </span> <span class='exterior'></span> </div> <img src='https:\/\/steamcommunity-a.akamaihd.net\/economy\/image\/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXX7gNTPcUxuxpJSXPbQv2S1MDeXkh6LBBOie3rKFRh16PKd2pDvozixtSOwaP2ar7SlzIA6sEo2rHCpdyhjAGxr0A6MHezetG0RZXdTA\/91fx68f' onerror=\"this.src='https:\/\/steamcommunity-a.akamaihd.net\/economy\/image\/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXX7gNTPcUxuxpJSXPbQv2S1MDeXkh6LBBOie3rKFRh16PKd2pDvozixtSOwaP2ar7SlzIA6sEo2rHCpdyhjAGxr0A6MHezetG0RZXdTA\/92fx68f';\" class='itemImg'></img> <img src='\/images\/st.png?1' class=\"stImg\" style=\"visibility:none;\"></img> <center style='pointer-events:none;'> <b id='value5' class='value' style="color: #57a857"> <span>$2.85</u></b> </center> </div> / \ / steamcommunity-a.akamaihd.net \ / economy \ / image \ / - 9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXX7gNTPcUxuxpJSXPbQv2S1MDeXkh6LBBOie3rKFRh16PKd2pDvozixtSOwaP2ar7SlzIA6sEo2rHCpdyhjAGxr0A6MHezetG0RZXdTA \ / 91fx68f' onerror = \ "this.src = 'https: \ / \ / steamcommunity-a <div style='background-color:#2E2E2E;' botNum='1' id='5' inspectLink='' valueType='Key' amount='52' original-name='Chroma 2 Case Key' title='<div style=\"margin:-8px -8px; padding:3px 10px; padding-bottom:5px; color:#57a857;font-size:16px;background:rgba(26,26,26, 1);\">Chroma 2 Case Key<\/div>' classId='927007517' value='2.85' class="itemImgDiv" clickable='true'> <div class='propsWrapper'> <span class='number'> <b id='num5'>x52</b> </span> <span class='exterior'></span> </div> <img src='https:\/\/steamcommunity-a.akamaihd.net\/economy\/image\/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXX7gNTPcUxuxpJSXPbQv2S1MDeXkh6LBBOie3rKFRh16PKd2pDvozixtSOwaP2ar7SlzIA6sEo2rHCpdyhjAGxr0A6MHezetG0RZXdTA\/91fx68f' onerror=\"this.src='https:\/\/steamcommunity-a.akamaihd.net\/economy\/image\/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXX7gNTPcUxuxpJSXPbQv2S1MDeXkh6LBBOie3rKFRh16PKd2pDvozixtSOwaP2ar7SlzIA6sEo2rHCpdyhjAGxr0A6MHezetG0RZXdTA\/92fx68f';\" class='itemImg'></img> <img src='\/images\/st.png?1' class=\"stImg\" style=\"visibility:none;\"></img> <center style='pointer-events:none;'> <b id='value5' class='value' style="color: #57a857"> <span>$2.85</u></b> </center> </div> / - 9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXX7gNTPcUxuxpJSXPbQv2S1MDeXkh6LBBOie3rKFRh16PKd2pDvozixtSOwaP2ar7SlzIA6sEo2rHCpdyhjAGxr0A6MHezetG0RZXdTA \ / 92fx68f '; \ "class =' itemImg '> </ img> <div style='background-color:#2E2E2E;' botNum='1' id='5' inspectLink='' valueType='Key' amount='52' original-name='Chroma 2 Case Key' title='<div style=\"margin:-8px -8px; padding:3px 10px; padding-bottom:5px; color:#57a857;font-size:16px;background:rgba(26,26,26, 1);\">Chroma 2 Case Key<\/div>' classId='927007517' value='2.85' class="itemImgDiv" clickable='true'> <div class='propsWrapper'> <span class='number'> <b id='num5'>x52</b> </span> <span class='exterior'></span> </div> <img src='https:\/\/steamcommunity-a.akamaihd.net\/economy\/image\/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXX7gNTPcUxuxpJSXPbQv2S1MDeXkh6LBBOie3rKFRh16PKd2pDvozixtSOwaP2ar7SlzIA6sEo2rHCpdyhjAGxr0A6MHezetG0RZXdTA\/91fx68f' onerror=\"this.src='https:\/\/steamcommunity-a.akamaihd.net\/economy\/image\/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXX7gNTPcUxuxpJSXPbQv2S1MDeXkh6LBBOie3rKFRh16PKd2pDvozixtSOwaP2ar7SlzIA6sEo2rHCpdyhjAGxr0A6MHezetG0RZXdTA\/92fx68f';\" class='itemImg'></img> <img src='\/images\/st.png?1' class=\"stImg\" style=\"visibility:none;\"></img> <center style='pointer-events:none;'> <b id='value5' class='value' style="color: #57a857"> <span>$2.85</u></b> </center> </div> 

From the div should get a json of the form:

 [{"botNum": "1","amount": "52", "name": "Chroma 2 Case Key", "value" : "2.85"}] 

How can I pass this to json?

  • In connection with the rules, you should rewrite your question in Russian! - Paulo Berezini
  • Explain your question. What exactly is the "transfer to json" problem? - rjhdby
  • I sparsed the page like this $ get_content = file_get_contents (' csgosell.com/phpLoaders/getInventory/getInventory.php ', false, $ context); The whole problem is that that page gives out html, not json and I can't figure out how to remake html in json read only about the tables and that decision was on js - Jackmin
  • issue your invalid html in the form of a Code block, not a quote, and bring it to the form that it actually looks like. Write the json structure you want to get. - teran
  • Corrected as it was said. - Jackmin

1 answer 1

You can come up with several options for solving this problem. The simplest is to use regular expressions. Other options may include casting the source html-code to the form of valid XML and working with it, or some kind of manual parsing.

Using regular expressions, the desired result can be obtained as follows (html-code in the data.txt file):

 $txt = file_get_contents("data.txt"); $fields = [ 'botNum' => "/botNum='([\d]+)'/", 'amount' => "/amount='([\d]+)'/", 'name' => "/name='(.+)'/", 'value' => "/value='(.+)'/", ]; $result = []; foreach($fields as $k => $rExp){ if(preg_match($rExp, $txt, $matches)){ $result[$k] = $matches[1]; } } print json_encode([$result]); 

At the output we get:

 [{"botNum":"1","amount":"52","name":"Chroma 2 Case Key","value":"2.85"}] 
  • Thank you for help. For some reason, if you take the code that lies in the subject, then everything is fine, and if you take some small problems with the name from the file, but the main thing is that you gave me an idea how to implement it. Thanks again - Jackmin