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