Regex replace between html tags not working well
I want to use this function to replace text between HTML tags:
<?php
function Obfuscate($html)
{
$html = str_replace(array(
"\r",
"\n",
" "
) , '', $html);
$html = preg_replace_callback_array([
'/<(a|b|button|center|div|em|fieldset|font|FONT|SPAN|form|h1|h2|h3|h4|h5|h6|i|noscript|ol|optgroup|option|p|small|span|strong|sub|sup|td|textarea|th)([^<]*)>([^<>&]+)<([\/]?)\\1/' => function ($m)
{
$r = Replace($m[3], "yes");
return "<" . $m[1] . $m[2] . ">" . $r . "<" . $m[4] . $m[1];
},
'/<(a|b|button|center|div|em|fieldset|font|FONT|SPAN|form|h1|h2|h3|h4|h5|h6|i|noscript|ol|optgroup|option|p|small|span|strong|sub|sup|td|textarea|th)([^<]*)>([^<>&]+)<([\/]?)\\1/' => function ($m)
{
$r = Replace($m[3], "yes");
return "<" . $m[1] . $m[2] . ">" . $r . "<" . $m[4] . $m[1];
}
], $html);
return $html;
}
function Replace($pain_text = "", $what)
{
$crypt = array(
"A" => "065",
"a" => "097",
"B" => "066",
"b" => "098",
"C" => "067",
"c" => "099",
"D" => "068",
"d" => "100",
"E" => "069",
"e" => "101",
"F" => "070",
"f" => "102",
"G" => "071",
"g" => "103",
"H" => "072",
"h" => "104",
"I" => "073",
"i" => "105",
"J" => "074",
"j" => "106",
"K" => "075",
"k" => "107",
"L" => "076",
"l" => "108",
"M" => "077",
"m" => "109",
"N" => "078",
"n" => "110",
"O" => "079",
"o" => "111",
"P" => "080",
"p" => "112",
"Q" => "081",
"q" => "113",
"R" => "082",
"r" => "114",
"S" => "083",
"s" => "115",
"T" => "084",
"t" => "116",
"U" => "085",
"u" => "117",
"V" => "086",
"v" => "118",
"W" => "087",
"w" => "119",
"X" => "088",
"x" => "120",
"Y" => "089",
"y" => "121",
"Z" => "090",
"z" => "122",
"0" => "048",
"1" => "049",
"2" => "050",
"3" => "051",
"4" => "052",
"5" => "053",
"6" => "054",
"7" => "055",
"8" => "056",
"9" => "057",
"&" => "038",
" " => "032",
"_" => "095",
"-" => "045",
"@" => "064",
"." => "046"
);
$r = "";
for ($i = 0;$i < strlen($pain_text);$i++)
{
$y = substr($pain_text, $i, 1);
if (array_key_exists($y, $crypt))
{
$rand1 = rand(1, 3);
if ($what == 'yes')
{
$r = $r . "<span style='font-size:0px;'>☠" . rand(1, 10) . "</span>" . "&#" . $crypt[$y] . ";" . "<span style='font-size:0px;'>" . rand(1, 10) . "</span>";
}
else
{
$r = $r . "&#" . $crypt[$y] . ";";
}
}
else
{
$r = $r . $y;
}
}
if ($what == 'yes')
{
$r = $r . "<span style='font-size:0px;'>☠</span>";
return $r;
}
else
{
return $r;
}
}
?>
This function works well obfuscating HTML code it takes text between HTML and encodes it. The problem is when I have an HTML code when we have two HTML tags inside each other like this examples
<p><b>TEXT 1</b> TEXT 2</p>
What it does is that it encodes only what is between the inner HTML tag and avoids the other text.
So TEXT 1 is encoded but TEXT 2 is ignored. I hope you can find me a solution.
How many English words
do you know?
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
how many words do you know
Powered by Examplum