nodeffect
nodeffect

Reputation: 1840

yii2: getting image path from string - "Class 'app\models\DOMXPath' not found"

I'm trying to get image path by using the codes below. It works on PHP without frameworks. However, when I try to use it on yii2 framework, I will have an error message "Class 'app\models\DOMXPath' not found".

$image_tag = "<img src='test.com/image.jpg' border='0' title='Click Here'>";
$xpath = new DOMXPath(@DOMDocument::loadHTML($image_tag));
$src = $xpath->evaluate("string(//img/@src)");

Is there a way to get image path using yii2 ?

Upvotes: 0

Views: 236

Answers (1)

Scopey
Scopey

Reputation: 6319

This looks like a namespacing issue. I assume you are in the app\models namespace but you're trying to use a class from the base namespace.

Try:

$xpath = new \DOMXPath(\DOMDocument::loadHTML($image_tag));
// OR:
use DOMXPath;
use DOMDocument;
$xpath = new DOMXPath(DOMDocument::loadHTML($image_tag));

You should also be using a try {} catch {} block for catching errors opposed to using the @ symbol, which can just make bug tracking hard in the future.

Upvotes: 1

Related Questions