PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 19, 2022

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

 March 19, 2022     image, php, yii, yii2     No comments   

Issue

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 ?


Solution

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.



Answered By - Scopey
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing