Asked  1 Year ago    Answers:  5   Viewed   19 times

i am working with a project that require the use of php simple html dom parser, and i need a way to add a custom attribute to a number of elements based on class name.

i am able to loop through the elements with a foreach loop, and it would be easy to set a standard attribute such as href, but i can't find a way to add a custom attribute.

the closest i can guess is something like:

foreach($html -> find(".myelems") as $element) {

but this doesn't work.

i have seen a number of other questions on similar topics, but they all suggest using an alternative method for parsing html (domdocument etc.). in my case this is not an option, as i must use simple html dom parser.



did you try it? try this example (sample: adding data tags).

include 'simple_html_dom.php';

$html_string = '
    <p class="myelems">text inside 1</p>
    <p class="myelems">text inside 2</p>
    <p class="myelems">text inside 3</p>
    <p>simple text 1</p>
    <p>simple text 2</p>

$html = str_get_html($html_string);
foreach($html->find('div p[class="myelems"]') as $key => $p_tags) {
    $p_tags->{'data-index'} = $key;

echo htmlentities($html);


    <p class="myelems" data-index="0">text inside 1</p> 
    <p class="myelems" data-index="1">text inside 2</p> 
    <p class="myelems" data-index="2">text inside 3</p> 
    <p>simple text 1</p> 
    <p>simple text 2</p> 
Friday, May 28, 2021

for case 1:

// create dom from url or file 
$html = file_get_html('');

// get the style attribute for the item
$style = $html->getelementbyid("id100")->getattribute('style');

// $style = background:url(/mycar1.jpg)
// you would now need to put it into a css parser or do some regular expression magic to get the values you need.

for case 2/3:

// create dom from url or file
$html = file_get_html('');

// get the style element
$style = $html->find('head',0)->find('style');

// $style now contains an array of style elements within the head. you will need to work out using attribute selectors what whether an element has a src attribute, if it does download the external css file and parse (using a css parser), if it doesnt then pass the innertext to the css parser.
Thursday, April 1, 2021

first, what i would do is also iterate each td's thru foreach. so that you'll be able to get which index number key it falls into. (note that of course its indexing is zero based so it actually starts at 0).

then inside the inner loop, just check if the class is null, then map it in the corresponding word 1 = one, 2 = two, etc...

rough example:

$map = array(1 => 'one', 2 => 'two', 3 => 'three');
foreach ($demo->find('tr') as $tr) { // loop each table row
    // then loop each td
    foreach($tr->find('td') as $i => $td) { // indexing starts at zero
        if($td->class == 'null') { // if its class is null
            echo $map[$i+1]; // map it to its corresponding word equivalent

so in this case, this would output three and then two. inside the second table row, the null lands on the third, inside the third row it lands into the second.

Thursday, April 1, 2021

if you know the exact string you want to replace you can use str_replace

$string = '<p>this is my super dupa text and it has my phone number: 083 23212212 32</p>';
$string = str_replace("phone number:", "<b>phone number:</b>", $string);

string now has 'phone number:' in bold. if you have a number of things to replace you can use str_replace with arrays. look at the examples on the php website here:

Saturday, May 29, 2021

you can get the html code using the snoopy class ( next code displays the html code inside of a <textarea> tag, then it displays the page itself, copy-paste next code in a php file and open it in your browser:

<!doctype html>
    <meta http-equiv="content-type" content="text/html; charset=euc-kr">
    <meta http-equiv="content-language" content="ko">
require("snoopy.class.php"); // ◄■■ get snoopy from
$snoopy = new snoopy;
$html = mb_convert_encoding( $snoopy->results, "utf-8", "euc-kr" ); // ◄■■ get html code.
echo "<textarea rows='25' cols='80'>$html</textarea>"; // ◄■■ display the html.
echo $html; // ◄■■ display the webpage.

the snoopy class is only one file, make sure the file is in the same directory your php file is.

Thursday, December 9, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :