Question:
My client often copy / paste news providers that contain HTML comments.
In other words, in HTML it doesn't matter and when inserting them they don't appear in the text editor, but I use the PHP mailer
which ends up sending this text and makes these comments visible.
Below is an example:
<!-- /* Font Definitions */ @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-... -->
What can I do so that, before inserting it into the database, it removes this tag?
Answer:
Why not use a preg_replace ?
<?
$string = 'a<!-- /* Font Definitions */ @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:1;
mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-
... -->b<!-- /* Font Definitions */ @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:1;
mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-... -->';
$string = preg_replace('/<!--.*?-->/s', '', $string);
echo $string; // imprime: ab
?>
The regular expression pattern /<!--.*?-->/s
will search for everything between the <!--
and -->
signs (including the signs) and remove from the string.
Note in the example above that there is only one letter "a" and one "b" outside the comment blocks. So, when doing the replace, only these two letters will remain in the string.