2

I want to access a custom attribute that I added to some elements in an HTML file, here's an example of the littleBox="somevalue" attribute

<div id="someId" littleBox="someValue">inner text</div>

The Following doesn't work:

foreach($html->find('div') as $element){
 echo $element;
 if(isset($element->type)){
 echo $element->littleBox;
   }
}

I saw an article with a similar problem, but I couldn't replicate it for some reason. Here is what I tried:

function retrieveValue($str){
if (stripos($str, 'littleBox')){//check if element has it
$var=preg_split("/littleBox=\"/",$str);
//echo $var[1];
$var1=preg_split("/\"/",$var[1]);
echo $var1[0];
}
else
return false;
}

When ever I call the retrieveValue() function, nothing happens. Is $element (in the first PHP example above) not a string? I don't know if I missed something but it's not returning anything.

Here's the script in it's entirety:

<?php
require("../../simplehtmldom/simple_html_dom.php");

if (isset($_POST['submit'])){

$html = file_get_html($_POST['webURL']);

// Find all images 
foreach($html->find('div') as $element){
    echo $element;
   if(isset($element->type)!= false){
    echo retrieveValue($element);
   }
}
}


function retrieveValue($str){
if (stripos($str, 'littleBox')){//check if element has it
$var=preg_split("/littleBox=\"/",$str);
//echo $var[1];
$var1=preg_split("/\"/",$var[1]);
return $var1[0];
}
else
return false;
}

?>

<form method="post">
Website URL<input type="text" name="webURL">
<br />
<input type="submit" name="submit">
</form>
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Philll_t
  • 4,267
  • 5
  • 45
  • 59

2 Answers2

4

Have you tried:

$html->getElementById("someId")->getAttribute('littleBox');

You could also use SimpleXML:

$html = '<div id="someId" littleBox="someValue">inner text</div>';
$dom = new DOMDocument;
$dom->loadXML($html);
$div = simplexml_import_dom($dom);
echo $div->attributes()->littleBox;

I would advice against using regex to parse html but shouldn't this part be like this:

$str = $html->getElementById("someId")->outertext;
$var = preg_split('/littleBox=\"/', $str);
$var1 = preg_split('/\"/',$var[1]);
echo $var1[0];

Also see this answer https://stackoverflow.com/a/8851091/1059001

Community
  • 1
  • 1
P. Galbraith
  • 2,477
  • 21
  • 24
  • Thank you for your answer, that is the one that I'm confused on (link you provided). I tried the same thing, but it doesn't return the value of the custom attribute. I'll try the id suggestion, but the id is something that may vary and I won't know the specific id. – Philll_t Mar 04 '12 at 23:58
  • See above $html->getElementById("someId")->outertext; – P. Galbraith Mar 05 '12 at 00:02
  • this is what I'm trying: `// Find all divs foreach($html->find('div') as $element){ $str = $element->outertext; $var = preg_split('/littleBox=\"/', $str); $var1 = preg_split('/\"/',$var[1]); echo $var1[0]; }` – Philll_t Mar 05 '12 at 00:14
  • You can do it with SimpleXML see my revised answer. I just tested it and it works. That way you don't need any external libraries. – P. Galbraith Mar 05 '12 at 00:16
  • Wow that's cool, just one more thing... I'm using an external file, `file_get_xml('file.ext')`, how how would you do it with the external file? The file is HTML. – Philll_t Mar 05 '12 at 00:27
  • `$html = file_get_contents(...path to file...)` or `$dom = new DOMDocument; $dom->loadHTMLFile(...path to file...)` – P. Galbraith Mar 05 '12 at 00:58
  • I'm soooo sorry, I thought I did. There you go though. Thanks again! – Philll_t Apr 02 '12 at 18:54
  • This $html->getElementById("someId")->getAttribute('littleBox'); does exactly what I want. Thanks buddy ! – Trung Bui Jan 24 '16 at 06:32
0

See that http://code.google.com/p/phpquery/ it's like jQuery but on php. Very strong library.

v.tsurka
  • 452
  • 6
  • 18