Skip to content

Instantly share code, notes, and snippets.

@yehchge
Last active May 4, 2016 09:14
Show Gist options
  • Save yehchge/7f42f839c9140165dd504464cd0592ec to your computer and use it in GitHub Desktop.
Save yehchge/7f42f839c9140165dd504464cd0592ec to your computer and use it in GitHub Desktop.
PHP HTML to BBCode
//20160429 HTML To BBCode
public function sHtmlToBbcode($html, $bbcode_uid = 0){
$tags = array(
'#<strong>(.*?)</strong>#si' => '[b]\\1[/b]',
'#<b>(.*?)</b>#si' => '[b]\\1[/b]',
'#<em>(.*?)</em>#si' => '[i]\\1[/i]',
'#<i>(.*?)</i>#si' => '[i]\\1[/i]',
'#<u>(.*?)</u>#si' => '[u]\\1[/u]',
'#<s>(.*?)</s>#si' => '[s]\\1[/s]',
'#<h2>(.*?)</h2>#si' => '[h1]\\1[/h1]',
'#<h3>(.*?)</h3>#si' => '[h2]\\1[/h2]',
'#<h4>(.*?)</h4>#si' => '[h3]\\1[/h3]',
'#<ul>(.*?)</ul>#si' => '[ul]\\1[/ul]',
'#<ol>(.*?)</ol>#si' => '[ol]\\1[/ol]',
'#<li>(.*?)</li>#si' => '[li]\\1[/li]',
'#&nbsp;#si' => ' ',
'#<center>(.*?)</center>#si' => '[center]\\1[/center]',
'#<br(.*?)>#si' => chr(13).chr(10),
'#<p>(.*?)</p>#si' => chr(13).chr(10).chr(13).chr(10).'\\1',
'#<font.*? color="(.*?)".*?>(.*?)</font>#si' => '[color=\\1]\\2[/color]',
'#<img.*? src="(.*?)".*?>#si' => '[img]\\1[/img]',
'#<a.*? href="(.*?)".*?>(.*?)</a>#si' => '[url=\\1]\\2[/url]',
//'#<code>(.*?)</code>#si' => '[code]\\1[/code]',
//'#<iframe style="(.*?)" id="ytplayer" type="text/html" width="534" height="401" src="(.*?)/embed/(.*?)" frameborder="0"/></iframe>#si' => '[youtube]\\3[/youtube]',
'#<span.*? style="(.*?)".*?>(.*?)</span>#si' => '[style=\\1]\\2[/style]',
//'#<a href="mailto:"(.*?)" title="Email (.*?)">(.*?)</a>#si' => '[email]\\1[/email]',
//'#<img src="(.*?) >#si' => '[img]\\1[/img]',
);
foreach ($tags as $search => $replace)
$html = preg_replace($search, $replace, $html);
//去除剩於標籤
$html= preg_replace('/<([^<>]*)>/', '',$html);
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment