Get the price of an item on Steam Community Market with PHP and Regex

Get the price of an item on Steam Community Market with PHP and Regex

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Parsing Steam Market Prices: Why Regex Fails and What to Do Instead

Attempting to scrape data from the Steam Community Market using PHP is a common task for developers building trading bots or market analysis tools. The core challenge often lies in how you interact with the page source. A frequent stumbling block appears when trying to extract specific price values using regular expressions. Consider a scenario where you have fetched a listing page and are targeting a span element containing both the total price and the fee:

function getInnerHTML($string, $tagname, $closetagname) {
    $pattern = "/<$tagname ?.*>(.*)<\/$closetagname>/";
    preg_match($pattern, $string, $matches);
    return $matches[1];
}

// Usage attempt
getInnerHTML($str, 'span class="market_listing_price market_listing_price_with_fee"', 'span');

The immediate issue with the pattern above is that it expects a self-closing or empty tag structure on the opening side, yet the HTML provided by Steam nests tags differently. In the actual DOM, you often see a parent span containing a child span for the price. If your regex looks for an opening tag followed immediately by content and then a closing tag of the same name in a flat hierarchy, it will fail to capture nested elements correctly. Furthermore, the pattern $tagname ?.*> attempts to handle optional attributes but doesn't account for the fact that HTML is hierarchical, not linear text. When you run preg_match against the raw string and return $matches[1], you are getting the content between the opening and closing tags of the parent span, which in this case includes the child price element but requires precise boundary logic.

A more robust approach for parsing HTML is to use a dedicated parser rather than relying on regular expressions. Steam's markup can be complex, containing nested spans, conditionals based on inventory count, and dynamic classes. Regular expressions are notoriously bad at handling hierarchical structures because they lack memory of the nesting depth. If you stick with regex, you must ensure your pattern correctly matches the opening tag including its attributes and then captures everything up to the corresponding closing tag, accounting for nested tags within that scope.

// A more reliable regex approach if absolutely necessary
// This handles optional attributes better but still struggles with nesting
$pattern = '/<span\s+class="market_listing_price market_listing_price_with_fee">(.*)<\/span>/';
preg_match($pattern, $string, $matches);
if (!empty($matches[1])) {
    // Trim whitespace to get the clean price like "$1.92"
    echo trim($matches[1]); 
}

However, even this regex solution is fragile. If Steam updates their CSS class names or changes how they wrap the price text in a different number of spans, your scraper breaks immediately. This is why experienced engineers prefer libraries like DOMDocument or SimpleHTMLDom. These tools parse the HTML string into a tree structure where you can navigate by tag name and attribute values reliably.

$dom = new DOMDocument();
libxml_use_internal_errors(true); // Suppress warnings from malformed HTML
$dom->loadHTML($string);

// Navigate to the specific element
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//span[contains(@class, "market_listing_price_with_fee")]');

foreach ($nodes as $node) {
    // Extract text content directly from the node
    echo trim($node->nodeValue); 
}

This method is resilient to whitespace variations and nested tags. The libxml_use_internal_errors(true) call is critical because Steam's HTML often contains validation errors that would otherwise crash a standard parser. By using XPath, you can select elements based on partial class matches, making your code future-proof against minor frontend tweaks. While regex might seem simpler for quick scripts, the trade-off is maintainability and stability. If you are building a production tool, the overhead of learning XPath outweighs the benefits of writing a brittle regular expression.

For those integrating this into a Laravel application, you might find utility in leveraging existing packages or creating a dedicated Model trait that handles the fetching and parsing logic, keeping your controllers clean and focused on business rules rather than HTML manipulation.

Our network