Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
2.5 KiB

* html2bbcode
function html2bbcode($s) {
// only keep newlines from source that are within pre tags
$s = stripnl_exceptinpre($s);
// Tags to Find
$htmltags = array(
'/\<img(.*?) src=\"(.*?)\" (.*?)\>/is',
'/\<a (.*?)href=\"(.*?)\"(.*?)\>(.*?)\<\/a\>/is',
'/\<span style=\"color:(.*?)\"\>(.*?)\<\/span\>/is',
'/\<span style=\"font-size:(.*?)\"\>(.*?)\<\/span\>/is',
'/\<video(.*?) src=\"(.*?)\" (.*?)\>(.*?)\<\/video\>/is',
'/\<audio(.*?) src=\"(.*?)\" (.*?)\>(.*?)\<\/audio\>/is',
'/\<iframe(.*?) src=\"(.*?)\" (.*?)\>(.*?)\<\/iframe\>/is',
// Replace with
$bbtags = array(
// Replace $htmltags in $text with $bbtags
$text = preg_replace ($htmltags, $bbtags, $s);
call_hooks('html2bbcode', $text);
// Strip all other HTML tags
$text = strip_tags($text);
return $text;
function stripnl_exceptinpre($string)
// First, check for <pre> tag
if(strpos($string, '<pre>') === false)
return str_replace("\n","", $string);
// If there is a <pre>, we have to split by line
// and manually replace the linebreaks
$strArr=explode("\n", $string);
// Loop over each line
foreach($strArr as $line)
{ // See if the line has a <pre>. If it does, set $preFound to true
if(strpos($line, "<pre>") !== false)
elseif(strpos($line, "</pre>") !== false)
// If we are in a pre tag, add line and also add \n, else add the line without \n
$output .= $line . "\n";
$output .= $line ;
return $output;