Jump to content

Recommended Posts

Hey guys, Im working on a bbCode script that uses the same tags to open and close. IE: ++Bold Test++ The first ++ would be replaced with <b> and the second with </b>. I came up with the following code.

 

<?php
function BBcode($tag, $identifier, $string){
$open = '<' . $tag . '>';
$close = '<' . $tag . '/>';
$current = $open;
$i = 0;
$totalTagsSplit = split($identifier, $string);
$totalTags = count($totalTagsSplit);
if ($totalTags >= 1){

for ($i = 0; $i <= $totalTags; $i++){
	$string = str_replace($identifier, $current, $string);
	if ($current == $open){
	$current = $close;
	echo "opening";
	} elseif ($current == $close){
	$current = $open;
	echo "closing";
	}
	$i++;
	}
return $string;
}

}
////
//$tag being the HTML tag you want to use.
//$identifier being the string used to open and close the tag.
//$string being the actual string you want to manipulate.
////
?>

But its replacing everything with the same opening tag and never closes it. can anyone see what i did wrong?

 

Andy

Link to comment
https://forums.phpfreaks.com/topic/51677-my-own-bb-code-gone-wrong/
Share on other sites

That's not BB, but ok.

I made the following script for an intern communication-area (where only admins are):

<?php
class SIMPLE_CODE {
	function simplecode($s) {
		$s = nl2br(htmlspecialchars($s));
		$ns = '';

		//underlined
		$pos = -1;
		while(($pos = strpos($s, '__')) !== false) {
			$b=false;
			$bs=0;
			$i=-1;
			$last=-1;
			while (($back = strpos($s, '\\', $i+1)) !== false) {
				if ($back < $pos) {
					if ($b === false) $b = true;
					else $b = false;
					$bs++;
					$last=$back;
					$i = $back;
				}else {
					$back = $last;
					break;
				}
			}

			if ($b===true) $bs--;
			$bs = $bs/2;

			if ($b===true) {
				$ns .= substr($s, 0, $pos-1-$bs).'__';
				//echo $ns.'<br /><br /><br />';
				$s = substr($s, $pos+2);
				continue;
			}else {
				$end_pos = strpos($s, '__', $pos+1);
				if ($end_pos !== false) {
					$ns .= substr($s, 0, $pos);
					$ns .= '<you>'.substr($s, $pos+2, ($end_pos-$pos-2)).'</you>';
					$s = substr($s, $end_pos+2);
				}else
					break;
			}
		}
		$ns .= $s;
		$s = $ns;
		$ns = '';
		//italic
		$pos = -1;
		while(($pos = strpos($s, '//')) !== false) {
			$b=false;
			$bs=0;
			$i=-1;
			$last=-1;
			while (($back = strpos($s, '\\', $i+1)) !== false) {
				if ($back < $pos) {
					if ($b === false) $b = true;
					else $b = false;
					$bs++;
					$last=$back;
					$i = $back;
				}else {
					$back = $last;
					break;
				}
			}

			if ($b===true) $bs--;
			$bs = $bs/2;

			if ($b===true) {
				$ns .= substr($s, 0, $pos-1-$bs).'//';
				//echo $ns.'<br /><br /><br />';
				$s = substr($s, $pos+2);
				continue;
			}else {
				$end_pos = strpos($s, '//', $pos+1);
				if ($end_pos !== false) {
					$ns .= substr($s, 0, $pos);
					$ns .= '<i>'.substr($s, $pos+2, ($end_pos-$pos-2)).'</i>';
					$s = substr($s, $end_pos+2);
				}else
					break;
			}
		}
		$ns .= $s;
		$s = $ns;
		$ns = '';
		//bold
		$pos = -1;
		while(($pos = strpos($s, '**')) !== false) {
			$b=false;
			$bs=0;
			$i=-1;
			$last=-1;
			while (($back = strpos($s, '\\', $i+1)) !== false) {
				if ($back < $pos) {
					if ($b === false) $b = true;
					else $b = false;
					$bs++;
					$last=$back;
					$i = $back;
				}else {
					$back = $last;
					break;
				}
			}

			if ($b===true) $bs--;
			$bs = $bs/2;

			if ($b===true) {
				$ns .= substr($s, 0, $pos-1-$bs).'**';
				//echo $ns.'<br /><br /><br />';
				$s = substr($s, $pos+2);
				continue;
			}else {
				$end_pos = strpos($s, '**', $pos+1);
				if ($end_pos !== false) {
					$ns .= substr($s, 0, $pos);
					$ns .= '<b>'.substr($s, $pos+2, ($end_pos-$pos-2)).'</b>';
					$s = substr($s, $end_pos+2);
				}else
					break;
			}
		}
		$ns .= $s;

		return $ns;
	}


	function format($txt) {
		$ns = '';
		$pos= -1;
		while (($pos = strpos($txt, '<?')) !== false) {
			$end_pos = strpos($txt, '?>', $pos+1);

			if ($end_pos !== false) {
				$next_pos = strpos($txt, '<?', $end_pos);
				if ($next_pos === false) $next_pos = strlen($txt);

				/* next_pos erneuern */
				//Suche nach '
				$qo = false;
				$q =-1;
				while (($q = strpos($txt, "'", $q+1)) !== false) {
					if ($qo === false) $qo = true;
					else $qo = false;

					if ($qo === false && $next_pos < $q && $ql < $next_pos) {
						$next_pos = strpos($txt, '?>', $q+1);
					}

					if ($qo === false) $ql = $q;
					else $ql = strlen($txt);
				}

				/* end_pos erneuern */
				//Suche nach '
				$qo = false;
				$q =-1;

				while (($q = strpos($txt, "'", $q+1)) !== false) {
					if ($q >= $next_pos) break;

					if ($qo === false) $qo = true;
					else $qo = false;

					if ($qo === false && $end_pos < $q && $q < $next_pos) {
						$end_pos = strpos($txt, '?>', $q+1);
					}
				}
				//Suche nach "
				$qo = false;
				$q =-1;

				while (($q = strpos($txt, "'", $q+1)) !== false) {
					if ($q >= $next_pos) break;

					if ($qo === false) $qo = true;
					else $qo = false;

					if ($qo === false && $end_pos < $q && $q < $next_pos) {
						$end_pos = strpos($txt, '?>', $q+1);
					}
				}


				$ns.= $this->simplecode(substr($txt, 0, $pos));

				$code = substr($txt, $pos, ($end_pos-$pos+2));
				$output_result = '';
				ob_start();
				highlight_string($code);
				$output_result = ob_get_contents();
				ob_end_clean();
				$ns .= $output_result;

				$e = false;
				if (substr($code, 0, 5) == '<?php') $code = substr($code, 5);
				elseif (substr($code, 0, 3) == '<?=') { $code = 'echo '.substr($code, 3); $e = true; }
				elseif (substr($code, 0, 2) == '<?') $code = substr($code, 2);

				$code = substr($code, 0, -2);
				if ($e === true) $code .= ';';

				$output_result = $code_result = '';
				ob_start();
				$code_result = @eval($code);
				$output_result = ob_get_contents();
				ob_end_clean();

				if ($code_result === false)
					$ns .= '<span class="error"><br />-Code contains errors</span>';
				else {
					$ns .= '<span class="info"><br />-Code contains no errors</span>';
					if (strlen($code_result) > 0) {
						(strlen($code_result) > 32) && $code_result = substr($code_result, 0, 32).'...';
						$ns .= '<span class="grey1"><br /><span class="bi">-Result:</span> '.htmlspecialchars($code_result).'</span>';
					}
				}
				if ($output_result !== false && strlen($output_result) > 0) {
					$ns .= '<span class="grey2"><br /><span class="bi">-Output:</span> ';
					if (strlen($output_result) > 32) $output_result = substr($output_result, 0, 32).'...';
					$ns .= htmlspecialchars($output_result);
					$ns .= '</span>';
				}	

				$txt = substr($txt, $end_pos+2);

			}else
				break;
		}
		$ns .= $this->simplecode($txt);

		return $ns;
	}
}
?>

Use it like this:

<?php
   $c = new SIMPLE_CODE;
   echo $c->format('**bold** //italic// __underlined__');
?>

 

//edit: If you want to use it for open posting, please remove the eval()-command

personally i made this :-) translates `b into <b>/</b>

 

<?php
function filter_colourize($string){
while(($pos=strpos($string,"`")) !== false){
  $tag=substr($string,$pos+1,1);
  $pre=substr($string,0,$pos);
  $post=substr($string,$pos+2);
  switch($tag){
   case "0":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:white;">';
   break;
   case "1":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:olive;">';
   break;
   case "2":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:orange;">';
   break;
   case "3":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:plum;">';
   break;
   case "4":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:pink;">';
   break;
   case "5":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:purple;">';
   break;
   case "6":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:red;">';
   break;
   case "7":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:gray;">';
   break;
   case "8":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:green;">';
   break;
   case "9":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:hotpink;">';
   break;
   case "!":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:silver;">';
   break;
   case "@":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:gold;">';
   break;
   case "#":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:lightslategray;">';
   break;
   case "$":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:steelblue;">';
   break;
   case "%":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:ivory;">';
   break;
   case "^":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:tan;">';
   break;
   case "&":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:teal;">';
   break;
   case "a":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:yellow;">';
   break;
   case "b":
    if(!$nestedtags[bold]){
     $nestedtags[bold]=true;
     $colour.='<b>';
    }else{
     $nestedtags[bold]=false;
     $colour.='</b>';
    }
   break;
   case "c":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:cornsilk;">';
   break;
   case "d":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:navy;">';
   break;
   case "e":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:aqua;">';
   break;
   case "f":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:cyan;">';
   break;
   case "g":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:beige;">';
   break;
   case "h":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:lime;">';
   break;
   case "i":
    if(!$nestedtags[italics]){
     $nestedtags[italics]=true;
     $colour.='<i>';
    }else{
     $nestedtags[italics]=false;
     $colour.='</i>';
    }
   break;
   case "j":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:blue;">';
   break;
   case "k":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:linen;">';
   break;
   case "l":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:azure;">';
   break;
   case "m":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:black;">';
   break;
   case "n":
    $colour.='<br>';
   break;
   case "o":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:magenta;">';
   break;
   case "p":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:brown;">';
   break;
   case "q":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:cadetblue;">';
   break;
   case "r":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:maroon;">';
   break;
   case "s":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:coral;">';
   break;
   case "t":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:darkseagreen;">';
   break;
   case "u":
    if(!$nestedtags[underline]){
     $nestedtags[underline]=true;
     $colour.='<u>';
    }else{
     $nestedtags[underline]=false;
     $colour.='</u>';
    }
   break;
   case "v":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:chocolate;">';
   break;
   case "w":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:crimson;">';
   break;
   case "x":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:bisque;">';
   break;
   case "y":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:orchid;">';
   break;
   case "z":
    if(!$nestedtags[colour]) $nestedtags[colour]=true;
    else $colour.='</span>';
    $colour.='<span style="color:peru;">';
   break;
  }
  $string=$pre.$colour.$post;
  unset($colour);
  $i=$pos+1;
}
if($nestedtags[italics]) $string .= '</i>';
if($nestedtags[underline]) $string .= '</u>';
if($nestedtags[bold]) $string .= '</b>';
if($nestedtags[colour]) $string .= '</span>';
return $string;
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.