symfonyparsingdomdomcrawler

Is it possible to search by regexp with Symfony Dom crawler?


The Dom Crawler Component is powerfull to parse html content, in its documentation describes basics selections (like filter('body > p')) or more complex xpath like //span[contains(@id, "article-")]

Is it possible to fetch elements by regular expression? Maybe something like that is available: filter('body')->filter('div.*-timeLabel-*') ?


Solution

  • in XPath 2.0, you can use matches:

    $crawler->filterXPath("//div[matches(@id, '*-timeLabel-*')]");
    

    but if you don't have that available, your best bet is to try and combine some of the other XPath methods, for example this should do the trick for your case:

    $crawler->filterXPath("//div[contains(@id, '*-timeLabel-*')]");