We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,620 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Search a div using php

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:

$html->find('$html->find('#divid');');
$html->find('div[id=divid]');

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

Inside this id there is this kind of code:

....%3Fe%3D1309211119%26ri%3D1024%26rs%3D85%26h%3.....

Any idea?

Thanks ind advance!

6
Contributors
32
Replies
4 Days
Discussion Span
1 Year Ago
Last Updated
33
Views
Question
Answered
bipies
Light Poster
35 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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?

diafol
Keep Smiling
Moderator
10,838 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,534
Skill Endorsements: 61

wow, tons of questions ;)

I'm almost newbie but almost only.

Well I'm using this

Website: http://sourceforge.net/projects/simplehtmldom/

return is a string with "invalid" characters like % but I need them all, the hole string is it possible?

bipies
Light Poster
35 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

You tested with what ? This one ?

$html->find('$html->find('#divid');');
$html->find('div[id=divid]');

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.

ko ko
Practically a Master Poster
673 posts since Jan 2009
Reputation Points: 120
Solved Threads: 152
Skill Endorsements: 1

Hi

I tested with both and nothing was returned,

and others like;

$html->find('a[class=miniature]');
$html->find('span[class=red]');
$html->find('img[class=borderx]');

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]');

bipies
Light Poster
35 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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?

bipies
Light Poster
35 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Perhaps try:

$html->find('#divid');
twiss
Veteran Poster
1,005 posts since Apr 2010
Reputation Points: 177
Solved Threads: 102
Skill Endorsements: 5

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?

bipies
Light Poster
35 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

A string with a hole in it?

Interesting class. Did you check the documentation; http://simplehtmldom.sourceforge.net/manual.htm ?

diafol
Keep Smiling
Moderator
10,838 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,534
Skill Endorsements: 61

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


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

checking right now

bipies
Light Poster
35 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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).

twiss
Veteran Poster
1,005 posts since Apr 2010
Reputation Points: 177
Solved Threads: 102
Skill Endorsements: 5

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

txt-file.txt

<?xml version="1.0"?>

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

And the PHP Code:

<?php
$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)); 
fclose($fh);

// 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:

<?php

$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";
}

?>

~John

cjohnweb
Junior Poster
102 posts since Apr 2010
Reputation Points: 26
Solved Threads: 8
Skill Endorsements: 0

Hi!

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']');

bipies
Light Poster
35 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Change one of the quote pairs tot double quotes.

twiss
Veteran Poster
1,005 posts since Apr 2010
Reputation Points: 177
Solved Threads: 102
Skill Endorsements: 5

Do as @twiss said, or escape it.

$result = $xml->xpath('div[@id=\'player\']');
ko ko
Practically a Master Poster
673 posts since Jan 2009
Reputation Points: 120
Solved Threads: 152
Skill Endorsements: 1

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 (http://us.php.net/manual/en/class.domxpath.php) 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).

<?php
  
  $file = "file.txt"; // xml formatted text file...
  $doc = new DOMDocument();
  $doc->loadHTMLFile($file);
  
  $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";
      }
    }
  }

?>
cjohnweb
Junior Poster
102 posts since Apr 2010
Reputation Points: 26
Solved Threads: 8
Skill Endorsements: 0

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!

bipies
Light Poster
35 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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;

preg_match('/(flv_url=).?*(&amp;)/is',$line_content,$return);
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:

preg_match("/^(flv_url=).?*(&amp;)/is",$content,$return);

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

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

Good luck!

cjohnweb
Junior Poster
102 posts since Apr 2010
Reputation Points: 26
Solved Threads: 8
Skill Endorsements: 0

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

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

Thanks thanks thanks ;)

bipies
Light Poster
35 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

It needs an = in between.

twiss
Veteran Poster
1,005 posts since Apr 2010
Reputation Points: 177
Solved Threads: 102
Skill Endorsements: 5

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.1487 seconds using 2.76MB