Last Updated: February 25, 2016
·
709
· gerardorochin

Translate html minimal code WYSIWYG editor to markdown

<?php

$msg = "<p><b>test</b></p><p><i>test</i></p><p><strike>test</strike></p><p><br></p><p><ul><li>test</li></ul></p><p><ol><li>test</li></ol></p>";

$html = array('<b>', '</b>', '<i>', '</i>', '<p>', '</p>', '<br>', '<ul>', '</ul>', '<li>', '</li>', '<ol>', '</ol>', '<strike>', '</strike>');
$markdown = array('**', '**', '*', '*', '', PHP_EOL, PHP_EOL, '', '', "* ", '', '', '', '~~', '~~');

echo str_replace($html, $markdown, $msg);

/**
    Original:
    <p><b>test</b></p><p><i>test</i></p><p><strike>test</strike></p><p><br></p><p><ul><li>test</li></ul></p><p><ol><li>test</li></ol></p>

   Result:
    **test**
    *test*
    ~~test~~


    * test
    * test
*/