Search code examples
phpdomxpath

DomXPath query to select links which don't contain "mailto"


I tried to get all the links except ones contains 'mailto'. I have tried millions of different things but nothing seems to work. My best attempt was //a[@href(not(contaıns('maılto')))] but it didn't work too. I also tried to get all links with //a and then with $domelement->getAttribute('href') but it returns the href='mailto:x@x.com' as x@x.com, not with 'mailto'. It automatically cuts the 'mailto' part I don't know why. Anyone has experience about this?

Thanks.


Solution

  • You were on the right track using not() and contains() however your syntax is incorrect (also, I'd use starts-with() instead).

    //a[not(starts-with(@href, 'mailto'))]
    

    See http://www.w3.org/TR/xpath/#function-starts-with