PHP - Replace - Highlited

Status
Niet open voor verdere reacties.

acid007

Gebruiker
Lid geworden
13 jun 2002
Berichten
775
PHP:
$msg = preg_replace ("/\[code\]((.|\n)*?)\[\/code\]/e", 'highlight_string("\\1","1")', $msg);

Zoals je hierboven ziet, de [ p h p ] word vervangen door een bepaald stukje HTML met highlighte_string daarin. Ik wil graag weten hoe ik dat doe, want ik heb dus nu wel de code die goed word weergeven maar ik wil:
tekst tekst
----------------
code (highlited)
----------------


Alvast bedankt
 
Ik ben niet zo thuis in stringfuncties, maar kun je niet eerst checken of er in een string
PHP:
 staat. Dat je dan al die streepjes print. Dan de string highlited zet tot [ / php ], dan weer streepjes print en verder gaat?

Hier staan de stringfuncties: [url]http://www.php.net/manual/nl/ref.strings.php[/url]
 
mocht ik toch het antwoord nog vinden dan hoor je het van me.

Maar hoe zit het hier in dit forum dan?? of is dat een hele somme vraag :d
 
Zo zit het hier:

PHP:
<?
      if (floor($phpversionnum) < 4) {
        $searcharray = array(
          "/(\[)(list)(=)(['\"]?)([^\"']*)(\\4])(.*)(\[\/list)(((=)(\\4)([^\"']*)(\\4]))|(\]))/siU",
          "/(\[)(list)(])(.*)(\[\/list\])/siU",
          "/(\[\*\])/siU",
          "/(\[)(url)(=)(['\"]?)(www\.)([^\"']*)(\\4)(.*)(\[\/url\])/siU",
          "/(\[)(url)(=)(['\"]?)([^\"']*)(\\4])(.*)(\[\/url\])/siU",
          "/(\[)(url)(])(www\.)([^\"]*)(\[\/url\])/siU",
          "/(\[)(url)(])([^\"]*)(\[\/url\])/siU",
          "/(\[)(php)(])(\r\n)*(.*)(\[\/php\])/siU",
          "/(\[)(code)(])(\r\n)*(.*)(\[\/code\])/siU",
          "/javascript:/si",
          "/about:/si",
          "/vbscript:/si"
        );

        $replacearray = array(
          "<ol type=\"\\5\">\\7</ol>",
          "<ul>\\4</ul>",
          "<li>",
          "<a href=\"http://www.\\6\" target=\"_blank\">\\8</a>",
          "<a href=\"\\5\" target=\"_blank\">\\7</a>",
          "<a href=\"http://www.\\5\" target=\"_blank\">\\5</a>",
          "<a href=\"\\4\" target=\"_blank\">\\4</a>",
          "</normalfont><blockquote><pre><smallfont>code:</smallfont><hr>\\5<hr></pre></blockquote><normalfont>",
          "</normalfont><blockquote><pre><smallfont>code:</smallfont><hr>\\5<hr></pre></blockquote><normalfont>",
          "java script:",
          "about :",
          "vbscript :"
        );
      } else {
        $searcharray = array(
          "/(\[)(list)(=)(['\"]?)([^\"']*)(\\4])(.*)(\[\/list)(((=)(\\4)([^\"']*)(\\4]))|(\]))/esiU",
          "/(\[)(list)(])(.*)(\[\/list\])/esiU",
          "/(\[)(url)(=)(['\"]?)([^\"']*)(\\4])(.*)(\[\/url\])/esiU",
          "/(\[)(url)(])([^\"]*)(\[\/url\])/esiU",
          "/(\[)(code)(])(\r\n)*(.*)(\[\/code\])/esiU",
          "/(\[)(php)(])(\r\n)*(.*)(\[\/php\])/esiU",
          "/javascript:/si",
          "/about:/si",
          "/vbscript:/si"
        );

        $replacearray = array(
          "createlists('\\7', '\\5')",
          "createlists('\\4')",
          "checkurl('\\5', '\\7')",
          "checkurl('\\4')",
          "stripbrsfromcode('\\5')",
          "phphighlite('\\5')",
          "java script:",
          "about :",
          "vbscript :"
        );
      }  // end version check
$bbcode=preg_replace($searcharray, $replacearray, $bbcode);







function phphighlite($code) {
  //PHP 4 only

  if (floor(phpversion())<4) {
    $buffer=$code;
  } else {
		$code = str_replace("<br>", "", $code);
		$code = str_replace("<br />", "", $code);
		$code = str_replace("&gt;", ">", $code);
		$code = str_replace("&lt;", "<", $code);

		$code = str_replace("&amp;", "&", $code);
		$code = str_replace('$', '\$', $code);
		$code = str_replace('\n', '\\\\n', $code);
		$code = str_replace('\r', '\\\\r', $code);
		$code = str_replace('\t', '\\\\t', $code);

		$code = stripslashes($code);

		if (!strpos($code,"<?") and substr($code,0,2)!="<?") {
			$code="<?\n".trim($code)."\n?>";
			$addedtags=1;
		}
		ob_start();
		$oldlevel=error_reporting(0);
		highlight_string($code);
		error_reporting($oldlevel);
		$buffer = ob_get_contents();
		ob_end_clean();
		if ($addedtags) {
		  $openingpos = strpos($buffer,'&lt;?');
		  $closingpos = strrpos($buffer, '?');
		  $buffer=substr($buffer, 0, $openingpos).substr($buffer, $openingpos+5, $closingpos-($openingpos+5)).substr($buffer, $closingpos+5);
		}
		$buffer = str_replace("&quot;", "\"", $buffer);
  }

  return "</normalfont><blockquote><pre><smallfont>PHP:</smallfont><hr>$buffer<hr></pre></blockquote><normalfont>";
}
?>
 
Oh wacht eens, nu heb ik ook het antwoord op m'n eigen vraag geloof ik :p
[edit]bij mij werkt het nogsteeds niet :/// naja ik ga eerst wel verder met een ander projectje :p[/edit]
 
Laatst bewerkt:
hij deed het half, maar stopte weer met de volgende foutmelding;

Warning: Unknown modifier 'c' in /usr/local/psa/home/vhosts/dutchwebmasters.net/httpdocs/site/artikelensysteem/read.php on line 49


en op dit regel staat


PHP:
$code = preg_replace($searcharray, $replacearray, $msg);
 
PHP:
$code = preg_replace ("/\[code\](.+?)\[\/code\]/is", '<b>\\1</b>', $msg);


werkt, hij doet het prima. Behalve; hij laat de <b> tags gewoon staan!
 
wat bedoel je aap?

Dit heb ik nu:

PHP:
<? 

$msg = "$show->text";

 


// $msg = preg_replace("/\[code\](.+?)\[\/code\]/is",'<hr>\\1', $msg);
$msg = preg_replace("/\[b\](.+?)\[\/b\]/is",'<b>\\1</b>', $msg);   
$msg = preg_replace("/\[i\](.+?)\[\/i\]/is",'<i>\\1</i>', $msg);   
$msg = preg_replace("/\[u\](.+?)\[\/u\]/is",'<u>\\1</u>', $msg);  
$msg = preg_replace("/\[img\](.+?)\[\/img\]/is",'<img src="\\1">', $msg); 
$msg = preg_replace("/\[url\](.+?)\[\/url\]/is",'<a href=\"\\1\">\\1</a>', $msg); 
$msg = preg_replace('/\[code\]((.|\n)*?)\[\/code\]/is',"<hr>\\1<hr>", $msg);


                
highlight_string($msg);
$msg2 = str_replace("\n", "<br />", $msg);
echo "$msg2";
?>
De replace werkt, alleen zet hij nu alles in "tekst" zegmaar in het document. Hij zet dus (bijv:

< h r >hier de code die men heeft ingevult < h r >


Erg jammer
 
Laatst bewerkt:
Erm, dat is toch ook de bedoeling? Er mag toch geen HTML gepost worden?
 
met wat hulp ben ik er EINDELIJK helemaal achter, het werkt nu helemaal;

PHP:
$bericht = $show->text;   
$bericht = htmlspecialchars($bericht);   
$bericht = str_replace("\n","<br>",$bericht);   

$bericht = eregi_replace("\[color=([^\[]*)\]([^\[]*)\[/color\]","<font color=\"\1\">\2</font>",$bericht);    
$bericht = eregi_replace("\[size=([^\[]*)\]([^\[]*)\[/size\]","<font size=\"\1\">\2</font>",$bericht);    
$bericht = eregi_replace("\[font=([^\[]*)\]([^\[]*)\[/font\]","<font face=\"\1\">\2</font>",$bericht);    
$bericht = eregi_replace("\[img height=([^\[]*)\ width=([^\[]*)\]([^\[]*)\[/img\]","<img src=\"\3\" height=\"\1\" width=\"\2\">",$bericht);    
$bericht = eregi_replace("\[img]([^\[]*)\[/img\]","<img src=\"\1\">",$bericht);    
$bericht = eregi_replace("\[flash=([^\[]*)\,([^\[]*)\]([^\[]*)\[/flash\]","<object classid=\"clsid: D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\1 height=\2><param name=movie value=\3><param name=play value=true><param name=loop value=true><param name=quality value=high><embed src=\3 width=\1 height=\2 play=true loop=true quality=high></embed></object>",$bericht);    
$bericht = eregi_replace("\[align=([^\[]*)\]([^\[]*)\[/align\]","<p align=\"\1\">\2</p>",$bericht);    
$bericht = eregi_replace("\[shadow=([^\[]*)\,([^\[]*)\,([^\[]*)\]([^\[]*)\[/shadow\]","<font style=\"Filter: Shadow(color=\1, Direction=\2); Width=\3px;\">\4</font>",$bericht);    
$bericht = eregi_replace("\[glow=([^\[]*)\,([^\[]*)\,([^\[]*)\]([^\[]*)\[/glow\]","<font style=\"Filter: Glow(color=\1, Strength=\2); Width=\3px;\">\4</font>",$bericht);    
$bericht = str_replace("[scroll]","<marquee>",$bericht);    
$bericht = str_replace("[/scroll]","</marquee>",$bericht);    
$bericht = str_replace("[hr]","<hr>",$bericht);    
$bericht = str_replace("[sub]", "<sub>", $bericht);     
$bericht = str_replace("[/sub]", "</sub>", $bericht);     
$bericht = str_replace("[tt]", "<tt>", $bericht);     
$bericht = str_replace("[/tt]", "</tt>", $bericht);     
$bericht = str_replace("[sup]", "<sup>", $bericht);     
$bericht = str_replace("[/sup]", "</sup>", $bericht);     
$bericht = str_replace("[s]","<s>",$bericht);    
$bericht = str_replace("[/s]","</s>",$bericht);    
$bericht = str_replace("[b]", "<b>",$bericht);    
$bericht = str_replace("[/b]","</b>",$bericht);    
$bericht = str_replace("[i]","<i>",$bericht);    
$bericht = str_replace("[/i]","</i>",$bericht);    
$bericht = str_replace("[u]","<u>",$bericht);    
$bericht = str_replace("[/u]","</u>",$bericht);    
$bericht = str_replace("[li]","<li>",$bericht);
$bericht = str_replace("[/li]","</li>",$bericht); 
$bericht = str_replace("[ulist]","<ul>",$bericht);    
$bericht = str_replace("[/ulist]","</ul>",$bericht);    
$bericht = eregi_replace("\[email\]([^\[]*)\[/email\]", "<a href=\"mailto:\1\">\1</a>",$bericht);    
$bericht = eregi_replace("\[email=([^\[]*)\]([^\[]*)\[/email\]", "<a href=\"mailto:\1\">\2</a>",$bericht);    
$bericht = str_replace("[quote]","<blockquote><span class=\"12px\">quote:</span><hr>",$bericht);    
$bericht = str_replace("[/quote]","<hr></blockquote>",$bericht);    
$bericht = str_replace("[code]","<blockquote><hr>",$bericht);    
$bericht = str_replace("[/code]","<hr></blockquote>",$bericht);    
$bericht = eregi_replace("\[url\]www.([^\[]*)\[/url\]", "<a href=\"http://www.\1\" target=_blank>\1</a>",$bericht);    
$bericht = eregi_replace("\[url\]([^\[]*)\[/url\]","<a href=\"\1\" target=_blank>\1</a>",$bericht);    
$bericht = eregi_replace("\[url=([^\[]*)\]([^\[]*)\[/url\]","<a href=\"\1\" target=_blank>\2</a>",$bericht);    
$bericht = eregi_replace("(^|[>[:space:]\n])([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])([<[:space:]\n]|$)","\1<a href=\"\2://\3\4\" target=\"_blank\">\2://\3\4</a>\5",$bericht); 
$bericht = ereg_replace('&nbsp;', ' ', $bericht); 

$bericht = ereg_replace('&gt;', '>', $bericht); 
$bericht = ereg_replace('&quot;', '"', $bericht); 



echo "$bericht";
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan