Skip to content

Instantly share code, notes, and snippets.

@creyer
Forked from siamkreative/html-to-slack.php
Created June 5, 2018 13:30
Show Gist options
  • Save creyer/656e572eb9e3cdea3034b821aecd061e to your computer and use it in GitHub Desktop.
Save creyer/656e572eb9e3cdea3034b821aecd061e to your computer and use it in GitHub Desktop.
Convert HTML string to Slack's own markup language (https://slack.zendesk.com/hc/en-us/articles/202288908-Formatting-your-messages)
<?php
/* Strip out un-supported HTML tags */
$content = strip_tags($content, '<strong><em><del><li><code><pre>');
/* Properly format message */
$content = str_replace(array('<strong>', '</strong>'), array('*', '*'), $content);
$content = str_replace(array('<em>', '</em>'), array('_', '_'), $content);
$content = str_replace(array('<del>', '</del>'), array('~', '~'), $content);
$content = str_replace(array('<li>', '</li>'), array('•', ''), $content);
$content = str_replace(array('<code>', '</code>'), array('`', '`'), $content);
$content = str_replace(array('<pre>', '</pre>'), array('```', '```'), $content);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment