Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question