1
1
1773337472020-12-11 15:51:40
PHP
177333747, 2020-12-11 15:51:40

How to encrypt tag attribute value?

Hello, I have a string with html code, I need to extract the value of a tag attribute and calculate the md5 hash from this value, and insert it back into the string. The result should be the modified string.
I could only insert a random hash code instead of attribute values. I can't figure out how to extract attribute values ​​and hash this particular value.

function hash()
{
    $html = '<!DOCTYPE html>
    <html lang="en">
    <head>
    <body>
        <p></p>
        <script src="/script.js"></script>
        <a href ="htttp//examle.com"></a>
        <p></p>
        <a></a>
        <img src="/image.png">
    </body>
    
    </html>';
    
    $dom = new DOMDocument;
    $dom->loadHTML($html);
    

    $anch = $dom->getElementsByTagName('a');
    $image = $dom->getElementsByTagName('img');
    $script = $dom->getElementsByTagName('script');
   
    foreach ($anch as $a) {
       
        $a->setAttribute('href', md5(rand()));
        
    }
   
    foreach ($image as $img) {

        $img->setAttribute('src', md5(rand()));
    }
    foreach ($script as $scr) {

        $scr->setAttribute('src', md5(rand()));
    }
    
    echo nl2br( htmlspecialchars($dom->saveHTML()));
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
uni_rush, 2020-12-11
@177333747

foreach ($anch as $a) {

    $anch_href = $a->getAttribute('href');
    $a->setAttribute('href', md5($anch_href));

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question