user3155139
user3155139

Reputation: 75

PHP simple html dom parser div id with variable

I am new with PHP and i have a little problem. How can I search with variable with simple html dom parser? My id is "ti" and there are several same named divs. I only need the first one. The code works if i put ti instead of $variable to code.

thanks!

<?php

$variable = "ti"

include_once 'simple_html_dom.php';
$html = file_get_html('http://myurl.here');
$ret = $html->find('div[id=$variable]', 0);
if ($ret) {
    echo $ret->innertext; 
}
?>

Upvotes: 6

Views: 7423

Answers (1)

Peter Bloomfield
Peter Bloomfield

Reputation: 5766

You need to use double quotation marks (i.e. " instead of ') to embed variables in PHP strings, like this:

$ret = $html->find("div[id=$variable]", 0);

Upvotes: 5

Related Questions