Tylko te. Ale już mam:
<?php
function getOpenPos($txt)
{
$temp = [];
$pos = strpos($txt, "[math]", 0);
while ($pos)
{
array_push($temp, $pos);
$pos = strpos($txt, "[math]", $pos + 1);
}
return $temp;
}
function getClosPos($txt)
{
$temp = [];
$pos = strpos($txt, "[/math]", 0);
while ($pos)
{
array_push($temp, $pos);
$pos = strpos($txt, "[/math]", $pos + 1);
}
return $temp;
}
function checkPossProgress($arr)
{
$cnt = count($arr);
for ($i = 0; $i < $cnt; $i++)
if ($arr[$i][0] > $arr[$i][1])
return false;
return true;
}
function removeTags(&$arr)
{
$cnt = count($arr);
for ($i = 0; $i < $cnt; $i++)
$arr[$i][0] += 6;
}
function combinePos($txt)
{
$opened = getOpenPos($txt);
$closed = getClosPos($txt);
$combined = [];
if ((count($opened) == count($closed)) and (count($opened) > 0))
{
$cnt = count($opened);
for ($i = 0; $i < $cnt; $i++)
array_push($combined, [$opened[$i], $closed[$i]]);
}
if (checkPossProgress($combined) == false)
return ["error"];
removeTags($combined);
return $combined;
}
function checkForOtherTags($arr)
{
$tags = ["[b]", "[i]", "[u]", "[quote", "[code]", "[list]", "[list=", "[img]", "[url]", "[url=", "[flash=", "[size=", "[color=",
"[/b]", "[/i]", "[/u]", "[/quote", "[/code]", "[/list]", "[/img]", "[/url]", "[/flash]", "[/size]", "[/color]"];
$cnt = count($arr);
$cnt2 = count($tags);
for ($i = 0; $i < $cnt; $i++)
for ($j = 0; $j < $cnt2; $j++)
if ((strpos($arr[$i], $tags[$j]) !== FALSE) or (strpos($arr[$i], $tags[$j]) !== FALSE))
return true;
return false;
}
function extractContent($txt)
{
$poss = combinePos($txt);
$arr = [];
$cnt = count($poss);
for ($i = 0; $i < $cnt; $i++)
array_push($arr, substr($txt, $poss[$i][0], $poss[$i][1] - $poss[$i][0]));
if (checkForOtherTags($arr) == true)
return ["error"];
return $arr;
}
$text = "abc[math][b]x^2+4[/math][math]2*4=8[/math]";
echo json_encode(extractContent($text));
?>