aboutsummaryrefslogtreecommitdiffstats
path: root/include/html2bbcode.php
blob: 8025c336bc9ca5b3aa7cf7ce6044af7bd90108e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php

/**
 * html2bbcode
 */


function html2bbcode($s) {


	// only keep newlines from source that are within pre tags

	$s = stripnl_exceptinpre($s);


	// Tags to Find

	$htmltags = array(
		'/\<pre\>(.*?)\<\/pre\>/is',
		'/\<p(.*?)\>/is',
		'/\<\/p\>/is',
		'/\<b\>(.*?)\<\/b\>/is',
		'/\<i\>(.*?)\<\/i\>/is',
		'/\<u\>(.*?)\<\/u\>/is',
		'/\<ul\>(.*?)\<\/ul\>/is',
		'/\<li\>(.*?)\<\/li\>/is',
		'/\<img(.*?)width: *([0-9]+)(.*?)height: *([0-9]+)(.*?)src=\"(.*?)\" (.*?)\>/is',
		'/\<img(.*?)height: *([0-9]+)(.*?)width: *([0-9]+)(.*?)src=\"(.*?)\" (.*?)\>/is',
		'/\<img(.*?)src=\"(.*?)\"(.*?)width: *([0-9]+)(.*?)height: *([0-9]+)(.*?)\>/is',
		'/\<img(.*?)src=\"(.*?)\"(.*?)height: *([0-9]+)(.*?)width: *([0-9]+)(.*?)\>/is',
		'/\<img(.*?) src=\"(.*?)\" (.*?)\>/is',
		'/\<div(.*?)\>(.*?)\<\/div\>/is',
		'/\<br(.*?)\>/is',
		'/\<strong\>(.*?)\<\/strong\>/is',
		'/\<a (.*?)href=\"(.*?)\"(.*?)\>(.*?)\<\/a\>/is',
		'/\<code\>(.*?)\<\/code\>/is',
		'/\<span style=\"color:(.*?)\"\>(.*?)\<\/span\>/is',
		'/\<span style=\"font-size:(.*?)\"\>(.*?)\<\/span\>/is',
		'/\<blockquote\>(.*?)\<\/blockquote\>/is',
		'/\<video(.*?) src=\"(.*?)\" (.*?)\>(.*?)\<\/video\>/is',
		'/\<audio(.*?) src=\"(.*?)\" (.*?)\>(.*?)\<\/audio\>/is',
		'/\<iframe(.*?) src=\"(.*?)\" (.*?)\>(.*?)\<\/iframe\>/is',

	);

	// Replace with

	$bbtags = array(
		'[code]$1[/code]',
		'',
		"\n",
		'[b]$1[/b]',
		'[i]$1[/i]',
		'[u]$1[/u]',
		'[list]$1[/list]',
		'[*]$1',
		'[img=$2x$4]$6[/img]',
		'[img=$4x$2]$6[/img]',
		'[img=$4x$6]$2[/img]',
		'[img=$6x$4]$2[/img]',
		'[img]$2[/img]',
		'$2',
		"\n",
		'[b]$1[/b]',
		'[url=$2]$4[/url]',
		'[code]$1[/code]',
		'[color="$1"]$2[/color]',
		'[size=$1]$2[/size]',
		'[quote]$1[/quote]',
		'[video]$1[/video]',
		'[audio]$1[/audio]',
		'[iframe]$1[/iframe]',
	);

	// 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);

    $output="";
    $preFound=false;

    // 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)
        {
            $preFound=true;
        }
        elseif(strpos($line, "</pre>") !== false)
        {
            $preFound=false;
        }
       
        // If we are in a pre tag, add line and also add \n, else add the line without \n
        if($preFound)
        {
            $output .= $line . "\n";
        }
        else
        {
            $output .= $line ;
        }
    }

    return $output;
}