]> git.mxchange.org Git - friendica.git/blob - include/html2bbcode.php
Merge remote-tracking branch 'friendika/master' into newui
[friendica.git] / include / html2bbcode.php
1 <?php
2
3 /**
4  * html2bbcode
5  */
6
7
8 function html2bbcode($s) {
9
10
11         // only keep newlines from source that are within pre tags
12
13         $s = stripnl_exceptinpre($s);
14
15
16         // Tags to Find
17
18         $htmltags = array(
19                 '/\<pre\>(.*?)\<\/pre\>/is',
20                 '/\<p(.*?)\>/is',
21                 '/\<\/p\>/is',
22                 '/\<b\>(.*?)\<\/b\>/is',
23                 '/\<i\>(.*?)\<\/i\>/is',
24                 '/\<u\>(.*?)\<\/u\>/is',
25                 '/\<ul\>(.*?)\<\/ul\>/is',
26                 '/\<li\>(.*?)\<\/li\>/is',
27                 '/\<img(.*?)width: *([0-9]+)(.*?)height: *([0-9]+)(.*?)src=\"(.*?)\" (.*?)\>/is',
28                 '/\<img(.*?)height: *([0-9]+)(.*?)width: *([0-9]+)(.*?)src=\"(.*?)\" (.*?)\>/is',
29                 '/\<img(.*?)src=\"(.*?)\"(.*?)width: *([0-9]+)(.*?)height: *([0-9]+)(.*?)\>/is',
30                 '/\<img(.*?)src=\"(.*?)\"(.*?)height: *([0-9]+)(.*?)width: *([0-9]+)(.*?)\>/is',
31                 '/\<img(.*?) src=\"(.*?)\" (.*?)\>/is',
32                 '/\<div(.*?)\>(.*?)\<\/div\>/is',
33                 '/\<br(.*?)\>/is',
34                 '/\<strong\>(.*?)\<\/strong\>/is',
35                 '/\<a (.*?)href=\"(.*?)\"(.*?)\>(.*?)\<\/a\>/is',
36                 '/\<code\>(.*?)\<\/code\>/is',
37                 '/\<span style=\"color:(.*?)\"\>(.*?)\<\/span\>/is',
38                 '/\<span style=\"font-size:(.*?)\"\>(.*?)\<\/span\>/is',
39                 '/\<blockquote\>(.*?)\<\/blockquote\>/is',
40                 '/\<video(.*?) src=\"(.*?)\" (.*?)\>(.*?)\<\/video\>/is',
41                 '/\<audio(.*?) src=\"(.*?)\" (.*?)\>(.*?)\<\/audio\>/is',
42                 '/\<iframe(.*?) src=\"(.*?)\" (.*?)\>(.*?)\<\/iframe\>/is',
43
44         );
45
46         // Replace with
47
48         $bbtags = array(
49                 '[code]$1[/code]',
50                 '',
51                 "\n",
52                 '[b]$1[/b]',
53                 '[i]$1[/i]',
54                 '[u]$1[/u]',
55                 '[list]$1[/list]',
56                 '[*]$1',
57                 '[img=$2x$4]$6[/img]',
58                 '[img=$4x$2]$6[/img]',
59                 '[img=$4x$6]$2[/img]',
60                 '[img=$6x$4]$2[/img]',
61                 '[img]$2[/img]',
62                 '$2',
63                 "\n",
64                 '[b]$1[/b]',
65                 '[url=$2]$4[/url]',
66                 '[code]$1[/code]',
67                 '[color="$1"]$2[/color]',
68                 '[size=$1]$2[/size]',
69                 '[quote]$1[/quote]',
70                 '[video]$1[/video]',
71                 '[audio]$1[/audio]',
72                 '[iframe]$1[/iframe]',
73         );
74
75         // Replace $htmltags in $text with $bbtags
76         $text = preg_replace ($htmltags, $bbtags, $s);
77
78         call_hooks('html2bbcode', $text);
79
80         // Strip all other HTML tags
81         $text = strip_tags($text);
82         return $text;
83
84 }
85
86 function stripnl_exceptinpre($string)
87 {
88     // First, check for <pre> tag
89     if(strpos($string, '<pre>') === false)
90     {
91         return str_replace("\n","", $string);
92     }
93
94     // If there is a <pre>, we have to split by line
95     // and manually replace the linebreaks
96
97     $strArr=explode("\n", $string);
98
99     $output="";
100     $preFound=false;
101
102     // Loop over each line
103     foreach($strArr as $line)
104     {    // See if the line has a <pre>. If it does, set $preFound to true
105         if(strpos($line, "<pre>") !== false)
106         {
107             $preFound=true;
108         }
109         elseif(strpos($line, "</pre>") !== false)
110         {
111             $preFound=false;
112         }
113        
114         // If we are in a pre tag, add line and also add \n, else add the line without \n
115         if($preFound)
116         {
117             $output .= $line . "\n";
118         }
119         else
120         {
121             $output .= $line ;
122         }
123     }
124
125     return $output;
126 }
127