< Back

HTML to BBcode convertor using powershell

Tue Sep 24, 2019 5:49 am

BBCode in used on phpBB and other CMS and forums to markup content in place of HTML. There are few online converters available to convert your HTML code to BBCode. I have created a powershell Script to do the same using string operations. You can modify this script as per your requirement and for custom BBCodes that you use on your forum.



If you have text that is formatted using HTML and need to post this to your platform you can use the converter to convert the tags.When using BBCode all HTML tags cannot be converted to BBCode , you need to remove these tags from your post. Then replace any valid HTML tags with the BBCode tags.



The conversion is done using -replace operations on the string as in example:

Code: Select all

$html = $html -replace '<i(.*?)>(.*?)<\\/i>','[i]$2[/i]'
Here the HTML code for italics <i> is converted to the BBCode format . If your forum does not allow for that BBCode you can change the code to strip the text of any tags:

Code: Select all

$html = $html -replace '<i(.*?)>(.*?)<\\/i>',''
If you are using any alternate/custom tags you can replace that in the code.





Here is the powershell script to convert HTML to BBCode: The first line in the script is used to get your source file or html document, You can replace the path here

$html=Get-Content -path $env:userprofile\\Documents\\file.txt to point to your file. After conversion it is saved to $env:userprofile\\Documents\\ and also to the clipboard. You can paste the converted text directly into any application.

Code: Select all

$html=Get-Content -path  $env:userprofile\\Documents\\file.txt
$html =$html -replace '<td(.*?)>(.*?)</td>','[td]$2[/td]'
$html =$html -replace '<tr(.*?)>(.*?)</tr>','[tr]$2[/tr]'
$html =$html -replace '<thead(.*?)>(.*?)</thead>','$2'
$html =$html -replace '<tbody(.*?)>(.*?)</tbody>','$2'
$html =$html -replace '<tfoot(.*?)>(.*?)</tfoot>','$2'
$html =$html -replace '<table(.*?)>(.*?)</table>','[table]$2[/table]'
$html = $html -replace '\\n',"`n"
$html = $html -replace '\\r\\r',"`r"
$html = $html -replace '$baseurl',''
$html = $html -replace '<h[1-7](.*?)>(.*?)<\\/h[1-7]>','[b]$2[/b]'
$html = $html -replace '<p>',"`r`n"
$html = $html -replace '<br(.*?)>',"`n"
$html = $html -replace '<textarea(.*?)>(.*?)<\\/textarea>','[code]$2[/code]'
$html = $html -replace '<b(.*?)>(.*?)<\\/b>','[b]$2[/b]'
$html = $html -replace '<i(.*?)>(.*?)<\\/i>','[i]$2[/i]'
$html = $html -replace '<u(.*?)>(.*?)<\\/u>','[u]$2[/u]'
$html = $html -replace '<em>(.*?)<\\/em>','[i]$1[/i]'
$html = $html -replace '<strong>(.*?)<\\/strong>','[b]$1[/b]'
$html = $html -replace '<cite>(.*?)<\\/cite>','[i]$1[/i]'
$html = $html -replace '<font color="(.*?)">(.*?)<\\/font>','[color=$1]$2[/color]'
$html = $html -replace '<font color=(.*?)>(.*?)<\\/font>','[color=$1]$2[/color]'
$html = $html -replace '<link(.*?)>/',''
$html = $html -replace '<li(.*?)>(.*?)<\\/li>','[*]$2'
$html = $html -replace '<ul(.*?)>','[list]'
$html = $html -replace '<\\/ul>','[/list]'
$html = $html -replace '<div>',"`r`n"
$html = $html -replace '<\\/div>',"`r`n"
$html = $html -replace '<img(.*?)src="(.*?)"(.*?)>','[img]$2[/img]'
$html = $html -replace '<a(.*?)href="(.*?)"(.*?)>(.*?)<\\/a>','[url=$2]$4[/url]'
$html = $html -replace '\\[url=http:\\/\\/(.*?)](.*?)\\[\\/url]','[url=http://$1]$2[/url]'
$html = $html -replace '<head>(.*?)<\\/head>',''
$html = $html -replace '<object>(.*?)<\\/object>',''
$html = $html -replace '<script(.*?)>(.*?)<\\/script>',''
$html = $html -replace '<style(.*?)>(.*?)<\\/style>',''
$html = $html -replace '<title>(.*?)<\\/title>',''
$html = $html -replace '<!--(.*?)-->',"`n"
$html = $html -replace '<(?:[^>"]*|(["]).*?\\1)*>',''
$html = $html -replace '&nbsp;'," "
New-Item -Path $env:userprofile\\Documents\\ -Name  "bbcode.txt" -ItemType "file" -Value $html
$html|clip