+3 votes
in Programming Languages by (6.8k points)

I have the following line in my PHP code:

$xpath = new DOMXPath(@DOMDocument::loadHTML($html));

When I run my code on Ubuntu 18 from command line, it gives the following error: "PHP Fatal error:  Uncaught Error: Class 'DOMXPath' not found ". I am using PHP 7.2. How can I fix it.

1 Answer

+1 vote
by (27.1k points)

You need to install the DOM extension to fix the error. Run one of the following commands to install the DOM extension.

For PHP7:
sudo apt-get install php7.0-xml

For PHP7.1:
sudo apt-get install php7.1-xml

For PHP7.2:
sudo apt-get install php7.2-xml

If you are not sure what version you should select, run sudo apt-get install php-dom to find what you needs to install.

Here is the output of 'sudo apt-get install php-dom'

Reading package lists... Done
Building dependency tree      
Reading state information... Done
Package php-dom is a virtual package provided by:
  php7.0-xml 7.0.28-0ubuntu0.16.04.1
xml 7.2.3-1ubuntu1
You should explicitly select one to install.