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>
1
  • Why exactly can't you use DOM? Commented Mar 5, 2012 at 0:13

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

8 Comments

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.
See above $html->getElementById("someId")->outertext;
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]; }
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.
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.
|
0

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

2 Comments

If you use phpquery you should know that it is pretty a dead project now. Since there haven't been updates for a few years.
does anyone know how to access custom attributes using the above stuff? Or a workaround?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.