/**
* @param string $sContent
* @return string
*/
function cleanUpBBCodesInRow($sContent = '') {
// Remove broken BBCodes, except for url, img, attach
$sContent = preg_replace_callback('#(\[/?([a-z_\-]+)[^]]*])#is',
function ($m = []) {
return empty($m[2]) ? $m[0] : ((in_array(strtolower($m[2]), ['url', 'img', 'attach'])) ? $m[0] : '');
}, trim($sContent)
);
// Repair URL-Tag fuckups
$sContent = preg_replace('#^\[/url]\s?#is', '', $sContent);
$sContent = preg_replace('#\[url=\'([^]]+)\']#si', '[url="\\1"]', $sContent);
$sContent = preg_replace('#\[url=["\']?([^]]+)["\']?]$#si', '\\1', $sContent);
$sContent = preg_replace('#^\[url=["\']?([^]]+)["\']?]$#si', '\\1', $sContent);
$sContent = preg_replace('#^\[url=["\']?([^]]+)["\']?]Link$#si', '[url="\\1"]Link[/url]', $sContent);
$sContent = preg_replace('#\[url=["\']?([^]]+)["\']?]\s*\[/url]#si', '[url="\\1"]Link[/url]', $sContent);
$sContent = preg_replace('#^\[/url]\s?#is', '', $sContent);
$sContent = preg_replace('#"$#', '', $sContent);
return trim($sContent);
}
/**
* @param string $sContent
* @return string|string[]|null
*/
function cleanUpBBCodesInContent($sContent = '') {
$sContent = preg_replace([
// Remove all old table tags
'#(\[/?table[^]]*]\r?\n?)#si',
// Remove unused bbcode tags
'#(\[([a-z_\-]+)]\s*\[/\\2])#si',
// Remove multiple line breaks
'#(\r?\n){3,}#s'
],
['', '', "\n\n"],
$sContent
);
return $sContent;
}
/**
* @param string $sContent
* @return string
*/
function getNewTableStruct($sContent = '') {
// Remove ugly bbcode fuckups from content
$sContent = cleanUpBBCodesInContent($sContent);
// Define tab stops and line break characters
$sLineBreak = "\n";
$sTabStopTD = " ";
$sTabStopTR = " ";
// Remove breaks and tab stops if option save_rows is active
if (isset($_POST['save_rows'])) {
$sLineBreak = $sTabStopTD = $sTabStopTR = '';
}
$sContentNew = '';
$bTableStart = false;
$aLines = explode("\n", $sContent);
foreach ($aLines as $idx => $sLine) {
$sLine = trim($sLine);
$sTRows = '';
if (preg_match('#\|+#', $sLine)) {
$tdFields = explode('|', $sLine);
$sTRows .= $sTabStopTR . '[tr]' . $sLineBreak;
foreach ($tdFields as $tdContent) {
$sTRows .= $sTabStopTD . '[td]' . cleanUpBBCodesInRow($tdContent) . '[/td]' . $sLineBreak;
}
$sTRows .= $sTabStopTR . '[/tr]';
$sLine = $sTRows;
if (!$bTableStart) {
$sContentNew .= '[table]' . "\n";
$bTableStart = true;
}
} else {
if ($bTableStart) {
$sContentNew .= '[/table]' . "\n";
$bTableStart = false;
}
}
$sContentNew .= $sLine . "\n";
}
return $sContentNew;
}