]> git.mxchange.org Git - friendica.git/blob - include/html2bbcode.php
iframes
[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(.*?) src=\"(.*?)\" (.*?)\>/is',
28                 '/\<div(.*?)\>(.*?)\<\/div\>/is',
29                 '/\<br(.*?)\>/is',
30                 '/\<strong\>(.*?)\<\/strong\>/is',
31                 '/\<a (.*?)href=\"(.*?)\"(.*?)\>(.*?)\<\/a\>/is',
32                 '/\<code\>(.*?)\<\/code\>/is',
33                 '/\<span style=\"color:(.*?)\"\>(.*?)\<\/span\>/is',
34                 '/\<span style=\"font-size:(.*?)\"\>(.*?)\<\/span\>/is',
35                 '/\<blockquote\>(.*?)\<\/blockquote\>/is',
36                 '/\<video(.*?) src=\"(.*?)\" (.*?)\>(.*?)\<\/video\>/is',
37                 '/\<audio(.*?) src=\"(.*?)\" (.*?)\>(.*?)\<\/audio\>/is',
38                 '/\<iframe(.*?) src=\"(.*?)\" (.*?)\>(.*?)\<\/iframe\>/is',
39
40         );
41
42         // Replace with
43
44         $bbtags = array(
45                 '[code]$1[/code]',
46                 '',
47                 "\n",
48                 '[b]$1[/b]',
49                 '[i]$1[/i]',
50                 '[u]$1[/u]',
51                 '[list]$1[/list]',
52                 '[*]$1',
53                 '[img]$2[/img]',
54                 '$2',
55                 "\n",
56                 '[b]$1[/b]',
57                 '[url=$2]$4[/url]',
58                 '[code]$1[/code]',
59                 '[color="$1"]$2[/color]',
60                 '[size=$1]$2[/size]',
61                 '[quote]$1[/quote]',
62                 '[video]$1[/video]',
63                 '[audio]$1[/audio]',
64                 '[iframe]$1[/iframe]',
65         );
66
67         // Replace $htmltags in $text with $bbtags
68         $text = preg_replace ($htmltags, $bbtags, $s);
69
70         call_hooks('html2bbcode', $text);
71
72         // Strip all other HTML tags
73         $text = strip_tags($text);
74         return $text;
75
76 }
77
78 function stripnl_exceptinpre($string)
79 {
80     // First, check for <pre> tag
81     if(strpos($string, '<pre>') === false)
82     {
83         return str_replace("\n","", $string);
84     }
85
86     // If there is a <pre>, we have to split by line
87     // and manually replace the linebreaks
88
89     $strArr=explode("\n", $string);
90
91     $output="";
92     $preFound=false;
93
94     // Loop over each line
95     foreach($strArr as $line)
96     {    // See if the line has a <pre>. If it does, set $preFound to true
97         if(strpos($line, "<pre>") !== false)
98         {
99             $preFound=true;
100         }
101         elseif(strpos($line, "</pre>") !== false)
102         {
103             $preFound=false;
104         }
105        
106         // If we are in a pre tag, add line and also add \n, else add the line without \n
107         if($preFound)
108         {
109             $output .= $line . "\n";
110         }
111         else
112         {
113             $output .= $line ;
114         }
115     }
116
117     return $output;
118 }
119