A "StripHTML()" method to produce a plain-text version?
Posted: Tue Mar 10, 2009 7:29 am
I made a function StripHTML(), which receives the HTML message block as a parameter and strips off all HTML, replacing certain tags by e.g. tab characters to simulate a "table".
The function is likely to be pretty incmplete yet, but it does work.
In 3.3.3 I had it in an extension class to the Swift class. In 4.0.0 I made it a separate function, but it would be better if it were a method within a class.
Might it be a good idea to have something like that as a standard facility in in SwiftMailer?
Usage:
Here is the current version of the code. It is a bit makeshift yet, but it works:
The function is likely to be pretty incmplete yet, but it does work.
In 3.3.3 I had it in an extension class to the Swift class. In 4.0.0 I made it a separate function, but it would be better if it were a method within a class.
Might it be a good idea to have something like that as a standard facility in in SwiftMailer?
Usage:
Code: Select all
$Msg->setBody($Content, "text/html");
$Msg->addPart(StripHtml ($Content), "text/plain"); Code: Select all
//------ StripHtml(): ------
function StripHtml ($Text)
{
$T = str_replace ("/\r\n/", "\n", $Text); // "\r\n" to "\n"
$T = preg_replace ("/[\x20\x9]*<td[^>]*>\n/" , "\t", $T); // "<td>\n" to "\t"
$T = preg_replace ("/[\x20\x9]*<td[^>]*>/", "\t", $T); // "<td>" to "\t"
$T = preg_replace ("/[\x20\x9]*<\/tr[^>]*>\n/", "\n", $T); // "</tr>\n" to "\n"
$T = preg_replace ("/[\x20\x9]*<\/tr[^>]*>/", "\n", $T); // "</tr>" to "\n"
$T = preg_replace ("/<\/t[^>]*>\n/", "<xxx>", $T); // mark "</td>\n", "<tr>\n" etc.
$T = preg_replace ("/<\/t[^>]*>/", "<xxx>", $T); // mark "</td>", "<tr>" etc.
$T = preg_replace ("/<t[^>]+>\n/", "<xxx>", $T); // mark "<table>", "<tr>" etc.
$T = preg_replace ("/[\x20\x9]*<xxx>/", "<xxx>", $T); // mark indentation before "<t...>"
$T = preg_replace ("/<br[^>]*>\n/", "\n", $T); // "<br>\n" to "\n"
$T = preg_replace ("/<\/p[^>]*>\n/", "\n", $T); // "</p>\n" to "\n"
$T = preg_replace ("/<\/p[^>]*>/", "\n", $T); // "</p>" to "\n"
$T = preg_replace ("/<\/h\d[^>]*>\n?/", "\n", $T); // "</h1>" etc. to "\n"
$T = preg_replace ("/<\/?b>/", "*", $T); // "<b>" and "</b>" to "*"
$T = preg_replace ("/<\/?i>/", "/", $T); // "<i>" and "</i>" to "/"
$T = preg_replace ("/<[^>]*>/", "", $T); // remove all other HTML-tags, ...
// ... including the temporary "<xxx>"
return ($T);
} // "StripHtml()"
//--------------------------