0

i hope you may be able to help me out. I am building a scrape script using simple html dom.

I have a few sites where i need to get the thumbnail path, name of the movie and some other stuffs. I have build me an admin panel where i save in plaintext the methods required to find that stuff based on the matching pattern.

Eg.

$movie_name = $result->children(0)->children(0)->innertext;

This works just like it supposed to work but when i save children(0)->children(0)->innertext in the database and then back into variable, eg,

$variable = "children(0)->children(0)->innertext";
$movie_name = $result->$variable;

it does not work.

I am pretty sure i am going horribly wrong about this, so please give me a hint how i could just save the methods in plaintext and then call them.

It must be stored in plaintext because the dom is frequently changing so i will be able to keep up with it.

Best regards.

Florin Andrei
  • 287
  • 2
  • 11

2 Answers2

0

You're looking for the PHP eval() function:

$movie_name = $result->eval($variable);

Having said that, be warned that eval is evil.

Instead, I would recommend xpath.

Hope this helps!

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
  • Just tryed eval it does not work. XPath i dont really know how to do these because i know the DOM children number where it will be found but not the tag name. Maybe you can provide me some example. Thanks. – Florin Andrei Jul 26 '17 at 03:27
-1

Got it, eval() was the answer. Since no user input is going to the eval() its pretty safe in my particular case. Just had to do some escaping and declaring the variable containing the method inside eval();

This piece of code works for me.

 $res_mov_url_e     = eval("\$res_mov_url = \$result->$movie_url;");

Anyway big thanks guys!

Florin Andrei
  • 287
  • 2
  • 11