Start New Discussion within our Web Development Community

Hi everybody!

Well my question is as follows, I need to search for a div with a specific ID using php under wordpress, till now I found images and links, but I need to find a specific div ID and copy it, for now tested with this:


and several more but with no results m I doing something wrong?

Inside this id there is this kind of code:


Any idea?

Thanks ind advance!

What class are you using for the $html->find? Not sure how this method is supposed to work. What does find return? What are the allowed parameters? In what format are the parameters?

You tested with what ? This one ?


Does it correct use ? In my opinion, your class wont' work while it is quoting, the first line. Furthermore, why the class is inside the same class as parameter.

Perhaps, you would not probably well read the instruction. I don't know what that snippet would suppose which function.


I tested with both and nothing was returned,

and others like;


are working fine, it must be something "stupid" but don know what, :(

PS: all them are precede by a variable, like: $picture = $html->find('img[class=borderx]');

okokok, I've foounf this inside the code mentioned before:

protected $self_closing_tags = array('img'=>1, 'br'=>1, 'input'=>1, 'meta'=>1, 'link'=>1, 'hr'=>1, 'base'=>1, 'embed'=>1, 'spacer'=>1);

wich, I supose, works with images, so I want to work with videos flv, wich are the strings I am searching for, what do you think?

no way :(
But thinking in another way, if I know how a string starts (file=) and how it ends (.flv) is there a way to copy/retrive the hole string?

I've check it but I think thah I'm messing my self :)

maybe is... echo $html->getElementById("div1")->???

checking right now

Yes, but looking at the source, that's just a wrapper of:

$html->find('#div1', 0);

Where the 0 makes it return the first one (I think, which seems useless to me, but they also included a getElementsById function, which returns all elements with the ID. Go figure).

Give XPath a try, here is a sample I have in my personal library:


<?xml version="1.0"?>

This line of information will be pulled because it is in between the body tags!

And the PHP Code:

$filename = "txt-file.txt"; // xml formatted text file...

// open the file and load contents into $string
$fh = fopen($filename, "r") or die("Can't open file");
$string = fread($fh, filesize($filename)); 

// Get it ready for XPath
$xml = new SimpleXMLElement($string);

// Specify your XPath query / expression
$result = $xml->xpath('/body');

// Loop through each result XPath has returned
while(list( ,$node) = each($result)) {
    echo '/body: ',$node,"\n";


So for your xpath, do something like:


$url = ""; // Set this to url

$string = file_get_contents($url);

// Get it ready for XPath
$xml = new SimpleXMLElement($string);

// Specify your XPath query / expression
$result = $xml->xpath('/body/div[@id='div-id']');

// Loop through each result XPath has returned
while(list( ,$node) = each($result)) {
    echo '/body: ',$node,"\n";




Sorry, it says this:

Parse error: syntax error, unexpected T_STRING in /home/*****/get.php on line 11

this line is:

$result = $xml->xpath('div[@id='player']');

That's because of the encapsulated apostrophes.

I'm not the best with XPath myself, but I've used it for some pretty slick things. I got the following code from ( and I edited it to pull the content of any div with an id of player.

You may need to find another way to load the file you are working with though (save file_get_contents($file); as a file, then open it with the LoadHTMLFile function).

  $file = "file.txt"; // xml formatted text file...
  $doc = new DOMDocument();
  $xpath = new DOMXpath($doc);
  // example 1: for everything with an id
  //$elements = $xpath->query("//*[@id]");
  // example 2: for node data in a selected id
  //$elements = $xpath->query("/html/body/div[@id='yourTagIdHere']");
  // example 3: same as above with wildcard
  $elements = $xpath->query("*/div[@id='player']");
  if (!is_null($elements)) {
    foreach ($elements as $element) {
  //    echo "<br/>[". $element->nodeName. "]";
      $nodes = $element->childNodes;
      foreach ($nodes as $node) {
        echo $node->nodeValue. "\n";


Guys I love you all! It begins to work :)

Any way, sorry if I'm messing up all the time, maybe I shpuld explain what is the goal, let's go:

I must to search inside a <div id="player" ........ </id> inside a determinated html remote file a string that begins with: flv_url= and ends with &amp;

I don't know really if this is the method or if its possible I only know those two constants in the string but I want to get the hole string, what do you thing? Is it possible? :S

Tahnks again!

If I understand properly, you want to find a <div> with an id of player, then you want to take the information out of it IF that information begins with "flv_url=" and ends with "&amp;"?

You will have to add some code at lines 22 - 25, something like:

$nodes = $element->childNodes;
foreach ($nodes as $node) {
$line_content $node->nodeValue;

if(!empty($return[0])){$results[] = $line_content; unset($return);}

You will probably have to make changes to that preg_match function. I know it has to start with a "/" slash, and end with "/is", the ".?*" is like a wild card matching everything in between. I'm just not very good with regex stuff. You may also mount (I think thats what its called) that beginning with a "^" char:


You might need to escape the =, & and ; chars.

Sorry, I am out of time. I'll check back end of today.

Good luck!

wich is the value for $line_content $node->nodeValue;

it show a "Parse error: syntax error, unexpected T_VARIABLE in"

Thanks thanks thanks ;)

This question has already been answered. Start a new discussion instead.