I am trying to fetch prices from the steam market, now this all works well, and it is returning an array, but there is one problem. CS:GO items such as StatTrak items or knives have either a star (★
or how Valve sends it "\u2605
") or the trademark logo (™
or how Valve sends it "\u2122
")
My array doesn't see them as these characters, but convert it to this instead:
â StatTrak⢠Karambit | Damascus Steel (Field-Tested)
But it should be:
★ StatTrak™ Karambit | Damascus Steel (Field-Tested)
This is how I fetch the info:
$url = "https://steamcommunity.com/market/search/render/?query=&start=0&count=99&&search_descriptions=0&sort_column=price&sort_dir=popular&appid=730&category_730_ItemSet%5B%5D=any&category_730_ProPlayer%5B%5D=any&category_730_StickerCapsule%5B%5D=any&category_730_TournamentTeam%5B%5D=any&category_730_Weapon%5B%5D=any";
$html = file_get_contents($url);
$html = json_decode($html, true);
$html = $html['results_html'];
$dom = new DOMDocument;
@$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
$itemname = $xpath->query('//span[@id="result_' . $q . '_name"]');
$itemprice = $xpath->query('//*[@id="result_' . $q . '"]/div[1]/div[2]/span[1]/span[1]');
In an loop, ofcourse, but that shouldn't matter right now. $q
is in the range 0-99.
How do I go about it getting the contents with the proper characters?
Use following code to replace special characters with the original characters.
$itemprice = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>', '<p>Â </p>', '&quot;', 'Â '), array('', '', '', '', '', '"',''), $dom->saveHTML()));
View the page source and check what your original character is replaced with, Then again replace them with the original characters using str_replace()
.