Wie kann man Code formatiert posten?

Dr Dau

ich wisch hier durch
Hallo!

Wie macht ihr dass eigentlich so mit dem posten von Code?

Ich füge den Code immer per copy&paste ein.
So lange ich gleich auf "Thema erstellen" bzw. "Antworten" klicke, ist auch alles OK.
Doch so bald ich erst auf "Vorschau" klicke (und dass mache ich fast immer) sind alle Leerzeilen und Zeileneinrückungen verschwunden.
Somit ist der Code praktisch unlesbar.

Bei ein paar Zeilen Code kann man es ja noch einfach wieder richten, aber wie sieht es z.b. bei 100 Zeilen Code aus?
Ich kann mir nicht vorstellen, dass andere den Code dann nochmal per Hand formatieren.

Gibt es da irgend einen Trick?
Oder klickt ihr alle einfach gleich auf "Thema erstellen" bzw. auf "Antworten".

Gruss Dr Dau
 
Also ich hab da bisher eigentlich keine Probleme mit gehabt.
Was mir schon ein paar Mal passiert ist war, dass zusaetzliche Zeilenumbrueche auftauchten weil die Datei einfach ein wenig kaesig war (wahrscheinlich weil ich mal unter Windows an der Datei gearbeitet hab und sie dann unter Linux gepostet hab oder so).

Mal testen, ich klick auch vorher auf Vorschau.

Mal ein Test mit Highlight-Tags:
PHP:
<?php
function formathtml($html)
{
	$tabs=0;
	$html_array=preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/',$html,-1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
	$html='';
	for ($x=0;$x<count($html_array);$x++)
		{
			if (substr($html_array[$x],0,2)=='</')
				{
					$tabs--;
				}
			for ($y=0;$y<$tabs;$y++)
				{
					$html.="\t";
				}
			$html.=$html_array[$x]."\r\n";
			if ((substr($html_array[$x],0,1)=='<') && (substr($html_array[$x],1,1)!='/'))
				{
					if ((substr($html_array[$x],1,1)!=' ') && (substr($html_array[$x],1,1)!='!') && (substr($html_array[$x],1,3)!='img') && (substr($html_array[$x],1,2)!='br') && (substr($html_array[$x],1,2)!='hr') && (substr($html_array[$x],1,5)!='input') && (substr($html_array[$x],1,4)!='link') && (substr($html_array[$x],1,4)!='meta') && (substr($html_array[$x],1,3)!='col') && (substr($html_array[$x],1,5)!='frame') && (substr($html_array[$x],1,7)!='isindex') && (substr($html_array[$x],1,5)!='param') && (substr($html_array[$x],1,4)!='area') && (substr($html_array[$x],1,4)!='base'))
						{
							$tabs++;
						}
				}
		}
	if ($tabs!=0)
		{
			$html.='<!--'.$tabs." open elements found-->\r\n";
		}
	return $html;
}

if (isset($_GET['class']))
	{
		$classfile=file('classes/'.$_GET['class'].'.class.php');
		$public_variables=array();
		$private_variables=array();
		$public_functions=array();
		$private_functions=array();
		for ($x=0;$x<count($classfile);$x++)
			{
				if (strpos($classfile[$x],'private')!==false)
					{
						if (strpos($classfile[$x],'function')!==false)
							{
								$private_functions[]=trim($classfile[$x]);
							}
						else
							{
								$private_variables[]=trim($classfile[$x]);
							}
					}
				if (strpos($classfile[$x],'public')!==false)
					{
						if (strpos($classfile[$x],'function')!==false)
							{
								$public_functions[]=trim($classfile[$x]);
							}
						else
							{
								$public_variables[]=trim($classfile[$x]);
							}
					}
			}
		$output='<b>Methods of the class '.$_GET['class'].'</b>';
		$output.='<hr>';
		/*$output.='<b>Public methods</b><br><br>';
		for ($x=0;$x<count($public_functions);$x++)
			{
						$output.='<a href="#'.substr($public_functions[$x],strpos($public_functions[$x],'function')+9,strpos($public_functions[$x],'(')-strpos($public_functions[$x],'function')-9).'">'.substr($public_functions[$x],strpos($public_functions[$x],'function')+9).'</a><br>';
			}
		$output.='<hr>';
		$output.='<b>Private methods</b><br><br>';
		for ($x=0;$x<count($private_functions);$x++)
			{
						$output.='<a href="#'.substr($private_functions[$x],strpos($private_functions[$x],'function')+9,strpos($private_functions[$x],'(')-strpos($private_functions[$x],'function')-9).'">'.substr($private_functions[$x],strpos($private_functions[$x],'function')+9).'</a><br>';
			}
		$output.='<hr>';*/
		if (count($public_variables)>0)
			{
				$output.='<b><i>Public variables</i></b><hr>';
				for ($x=0;$x<count($public_variables);$x++)
					{
						$output.='<div>';
						$output.='<a name="'.substr($public_variables[$x],strpos($public_variables[$x],'public')+7,strpos($public_variables[$x],';')-strpos($public_variables[$x],'public')-7).'"><i>'.substr($public_variables[$x],strpos($public_variables[$x],'public')+7,strpos($public_variables[$x],';')-strpos($public_variables[$x],'public')-7).'</i></a><br><br>';
						$output.='<b><u>Information:</u></b><br>';
						$output.='<div>';
						$output.='<br><br>';
						$output.='</div>';
						$output.='<br><a href="#">Back to top</a><hr>';
						$output.='</div>';
					}
			}
		if (count($private_variables)>0)
			{
				$output.='<b><i>Private variables</i></b><hr>';
				for ($x=0;$x<count($private_variables);$x++)
					{
						$output.='<div>';
						$output.='<a name="'.substr($private_variables[$x],strpos($private_variables[$x],'private')+8,strpos($private_variables[$x],';')-strpos($private_variables[$x],'private')-8).'"><i>'.substr($private_variables[$x],strpos($private_variables[$x],'private')+8,strpos($private_variables[$x],';')-strpos($private_variables[$x],'private')-8).'</i></a><br><br>';
						$output.='<b><u>Information:</u></b><br>';
						$output.='<div>';
						$output.='<br><br>';
						$output.='</div>';
						$output.='<br><a href="#">Back to top</a><hr>';
						$output.='</div>';
					}
			}
		if (count($public_functions)>0)
			{
				$output.='<b><i>Public methods</i></b><hr>';
				for ($x=0;$x<count($public_functions);$x++)
					{
						$output.='<div>';
						$output.='<a name="'.substr($public_functions[$x],strpos($public_functions[$x],'function')+9,strpos($public_functions[$x],'(')-strpos($public_functions[$x],'function')-9).'"><i>'.substr($public_functions[$x],strpos($public_functions[$x],'function')+9).'</i></a><br><br>';
						$output.='<b><u>Information:</u></b><br>';
						$output.='<div>';
						$output.='<br><br>';
						$output.='</div>';
						$parameters=substr($public_functions[$x],strpos($public_functions[$x],'(')+1,strpos($public_functions[$x],')')-strpos($public_functions[$x],'(')-1);
						$parameters=explode(',',$parameters);
						if (!empty($parameters[0]))
							{
								$output.='<b><u>Parameters:</u></b><br>';
								$output.='<div>';
								for ($y=0;$y<count($parameters);$y++)
									{
										if (strpos($parameters[$y],'=')!==false)
											{
												$parameters[$y]=explode('=',$parameters[$y]);
												$output.='<b>'.$parameters[$y][0].'</b>';
												$output.='is optional, default is '.$parameters[$y][1].'<br><br>';
											}
										else
											{
												$output.='<b>'.$parameters[$y].'</b><br><br>';
											}
									}
								$output.='</div>';
							}
						$output.='<b><u>Returns:</u></b><br>';
						$output.='<div>';
						$output.='<br><br>';
						$output.='</div>';
						$output.='<!--';
						$output.='<b><u>Example:</u></b><br>';
						$output.='<div>';
						$output.='<br><br>';
						$output.='</div>';
						$output.='-->';
						$output.='<br><a href="#">Back to top</a><hr>';
						$output.='</div>';
					}
			}
		if (count($private_functions)>0)
			{
				$output.='<b><i>Private methods</i></b><hr>';
				for ($x=0;$x<count($private_functions);$x++)
					{
						$output.='<div>';
						$output.='<a name="'.substr($private_functions[$x],strpos($private_functions[$x],'function')+9,strpos($private_functions[$x],'(')-strpos($private_functions[$x],'function')-9).'"><i>'.substr($private_functions[$x],strpos($private_functions[$x],'function')+9).'</i></a><br><br>';
						$output.='<b><u>Information:</u></b><br>';
						$output.='<div>';
						$output.='<br><br>';
						$output.='</div>';
						$parameters=substr($private_functions[$x],strpos($private_functions[$x],'(')+1,strpos($private_functions[$x],')')-strpos($private_functions[$x],'(')-1);
						$parameters=explode(',',$parameters);
						if (!empty($parameters[0]))
							{
								$output.='<b><u>Parameters:</u></b><br>';
								$output.='<div>';
								for ($y=0;$y<count($parameters);$y++)
									{
										if (strpos($parameters[$y],'=')!==false)
											{
												$parameters[$y]=explode('=',$parameters[$y]);
												$output.='<b>'.$parameters[$y][0].'</b>';
												$output.='is optional, default is '.$parameters[$y][1].'<br><br>';
											}
										else
											{
												$output.='<b>'.$parameters[$y].'</b><br><br>';
											}
									}
								$output.='</div>';
							}
						$output.='<b><u>Returns:</u></b><br>';
						$output.='<div>';
						$output.='<br><br>';
						$output.='</div>';
						$output.='<!--';
						$output.='<b><u>Example:</u></b><br>';
						$output.='<div>';
						$output.='<br><br>';
						$output.='</div>';
						$output.='-->';
						$output.='<br><a href="#">Back to top</a><hr>';
						$output.='</div>';
					}
			}
		/*if (!file_exists('docs/'.$_GET['class'].'.html'))
			{
				$docfile=fopen('docs/'.$_GET['class'].'.html','w');
				fwrite($docfile,formathtml($output));
				fclose($docfile);
				echo 'Documentation template written';
			}
		else
			{
				echo 'Documentation template already exists';
			}*/
		echo formathtml($output);
	}
?>
Und das gleiche mit PHP-Tags:
PHP:
<?php
function formathtml($html)
{
	$tabs=0;
	$html_array=preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/',$html,-1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
	$html='';
	for ($x=0;$x<count($html_array);$x++)
		{
			if (substr($html_array[$x],0,2)=='</')
				{
					$tabs--;
				}
			for ($y=0;$y<$tabs;$y++)
				{
					$html.="\t";
				}
			$html.=$html_array[$x]."\r\n";
			if ((substr($html_array[$x],0,1)=='<') && (substr($html_array[$x],1,1)!='/'))
				{
					if ((substr($html_array[$x],1,1)!=' ') && (substr($html_array[$x],1,1)!='!') && (substr($html_array[$x],1,3)!='img') && (substr($html_array[$x],1,2)!='br') && (substr($html_array[$x],1,2)!='hr') && (substr($html_array[$x],1,5)!='input') && (substr($html_array[$x],1,4)!='link') && (substr($html_array[$x],1,4)!='meta') && (substr($html_array[$x],1,3)!='col') && (substr($html_array[$x],1,5)!='frame') && (substr($html_array[$x],1,7)!='isindex') && (substr($html_array[$x],1,5)!='param') && (substr($html_array[$x],1,4)!='area') && (substr($html_array[$x],1,4)!='base'))
						{
							$tabs++;
						}
				}
		}
	if ($tabs!=0)
		{
			$html.='<!--'.$tabs." open elements found-->\r\n";
		}
	return $html;
}

if (isset($_GET['class']))
	{
		$classfile=file('classes/'.$_GET['class'].'.class.php');
		$public_variables=array();
		$private_variables=array();
		$public_functions=array();
		$private_functions=array();
		for ($x=0;$x<count($classfile);$x++)
			{
				if (strpos($classfile[$x],'private')!==false)
					{
						if (strpos($classfile[$x],'function')!==false)
							{
								$private_functions[]=trim($classfile[$x]);
							}
						else
							{
								$private_variables[]=trim($classfile[$x]);
							}
					}
				if (strpos($classfile[$x],'public')!==false)
					{
						if (strpos($classfile[$x],'function')!==false)
							{
								$public_functions[]=trim($classfile[$x]);
							}
						else
							{
								$public_variables[]=trim($classfile[$x]);
							}
					}
			}
		$output='<b>Methods of the class '.$_GET['class'].'</b>';
		$output.='<hr>';
		/*$output.='<b>Public methods</b><br><br>';
		for ($x=0;$x<count($public_functions);$x++)
			{
						$output.='<a href="#'.substr($public_functions[$x],strpos($public_functions[$x],'function')+9,strpos($public_functions[$x],'(')-strpos($public_functions[$x],'function')-9).'">'.substr($public_functions[$x],strpos($public_functions[$x],'function')+9).'</a><br>';
			}
		$output.='<hr>';
		$output.='<b>Private methods</b><br><br>';
		for ($x=0;$x<count($private_functions);$x++)
			{
						$output.='<a href="#'.substr($private_functions[$x],strpos($private_functions[$x],'function')+9,strpos($private_functions[$x],'(')-strpos($private_functions[$x],'function')-9).'">'.substr($private_functions[$x],strpos($private_functions[$x],'function')+9).'</a><br>';
			}
		$output.='<hr>';*/
		if (count($public_variables)>0)
			{
				$output.='<b><i>Public variables</i></b><hr>';
				for ($x=0;$x<count($public_variables);$x++)
					{
						$output.='<div>';
						$output.='<a name="'.substr($public_variables[$x],strpos($public_variables[$x],'public')+7,strpos($public_variables[$x],';')-strpos($public_variables[$x],'public')-7).'"><i>'.substr($public_variables[$x],strpos($public_variables[$x],'public')+7,strpos($public_variables[$x],';')-strpos($public_variables[$x],'public')-7).'</i></a><br><br>';
						$output.='<b><u>Information:</u></b><br>';
						$output.='<div>';
						$output.='<br><br>';
						$output.='</div>';
						$output.='<br><a href="#">Back to top</a><hr>';
						$output.='</div>';
					}
			}
		if (count($private_variables)>0)
			{
				$output.='<b><i>Private variables</i></b><hr>';
				for ($x=0;$x<count($private_variables);$x++)
					{
						$output.='<div>';
						$output.='<a name="'.substr($private_variables[$x],strpos($private_variables[$x],'private')+8,strpos($private_variables[$x],';')-strpos($private_variables[$x],'private')-8).'"><i>'.substr($private_variables[$x],strpos($private_variables[$x],'private')+8,strpos($private_variables[$x],';')-strpos($private_variables[$x],'private')-8).'</i></a><br><br>';
						$output.='<b><u>Information:</u></b><br>';
						$output.='<div>';
						$output.='<br><br>';
						$output.='</div>';
						$output.='<br><a href="#">Back to top</a><hr>';
						$output.='</div>';
					}
			}
		if (count($public_functions)>0)
			{
				$output.='<b><i>Public methods</i></b><hr>';
				for ($x=0;$x<count($public_functions);$x++)
					{
						$output.='<div>';
						$output.='<a name="'.substr($public_functions[$x],strpos($public_functions[$x],'function')+9,strpos($public_functions[$x],'(')-strpos($public_functions[$x],'function')-9).'"><i>'.substr($public_functions[$x],strpos($public_functions[$x],'function')+9).'</i></a><br><br>';
						$output.='<b><u>Information:</u></b><br>';
						$output.='<div>';
						$output.='<br><br>';
						$output.='</div>';
						$parameters=substr($public_functions[$x],strpos($public_functions[$x],'(')+1,strpos($public_functions[$x],')')-strpos($public_functions[$x],'(')-1);
						$parameters=explode(',',$parameters);
						if (!empty($parameters[0]))
							{
								$output.='<b><u>Parameters:</u></b><br>';
								$output.='<div>';
								for ($y=0;$y<count($parameters);$y++)
									{
										if (strpos($parameters[$y],'=')!==false)
											{
												$parameters[$y]=explode('=',$parameters[$y]);
												$output.='<b>'.$parameters[$y][0].'</b>';
												$output.='is optional, default is '.$parameters[$y][1].'<br><br>';
											}
										else
											{
												$output.='<b>'.$parameters[$y].'</b><br><br>';
											}
									}
								$output.='</div>';
							}
						$output.='<b><u>Returns:</u></b><br>';
						$output.='<div>';
						$output.='<br><br>';
						$output.='</div>';
						$output.='<!--';
						$output.='<b><u>Example:</u></b><br>';
						$output.='<div>';
						$output.='<br><br>';
						$output.='</div>';
						$output.='-->';
						$output.='<br><a href="#">Back to top</a><hr>';
						$output.='</div>';
					}
			}
		if (count($private_functions)>0)
			{
				$output.='<b><i>Private methods</i></b><hr>';
				for ($x=0;$x<count($private_functions);$x++)
					{
						$output.='<div>';
						$output.='<a name="'.substr($private_functions[$x],strpos($private_functions[$x],'function')+9,strpos($private_functions[$x],'(')-strpos($private_functions[$x],'function')-9).'"><i>'.substr($private_functions[$x],strpos($private_functions[$x],'function')+9).'</i></a><br><br>';
						$output.='<b><u>Information:</u></b><br>';
						$output.='<div>';
						$output.='<br><br>';
						$output.='</div>';
						$parameters=substr($private_functions[$x],strpos($private_functions[$x],'(')+1,strpos($private_functions[$x],')')-strpos($private_functions[$x],'(')-1);
						$parameters=explode(',',$parameters);
						if (!empty($parameters[0]))
							{
								$output.='<b><u>Parameters:</u></b><br>';
								$output.='<div>';
								for ($y=0;$y<count($parameters);$y++)
									{
										if (strpos($parameters[$y],'=')!==false)
											{
												$parameters[$y]=explode('=',$parameters[$y]);
												$output.='<b>'.$parameters[$y][0].'</b>';
												$output.='is optional, default is '.$parameters[$y][1].'<br><br>';
											}
										else
											{
												$output.='<b>'.$parameters[$y].'</b><br><br>';
											}
									}
								$output.='</div>';
							}
						$output.='<b><u>Returns:</u></b><br>';
						$output.='<div>';
						$output.='<br><br>';
						$output.='</div>';
						$output.='<!--';
						$output.='<b><u>Example:</u></b><br>';
						$output.='<div>';
						$output.='<br><br>';
						$output.='</div>';
						$output.='-->';
						$output.='<br><a href="#">Back to top</a><hr>';
						$output.='</div>';
					}
			}
		/*if (!file_exists('docs/'.$_GET['class'].'.html'))
			{
				$docfile=fopen('docs/'.$_GET['class'].'.html','w');
				fwrite($docfile,formathtml($output));
				fclose($docfile);
				echo 'Documentation template written';
			}
		else
			{
				echo 'Documentation template already exists';
			}*/
		echo formathtml($output);
	}
?>

Nachtrag: Sieht so aus als waere alles okay. Ich nutz grad den Firefox 1.5.0.2 unter WindowsXP/SP2.
Aber bisher hatte ich nie das von Dir geschilderte Problem, unabhaengig von OS und Browser.
 
Hmm, eigentlich erstelle ich meinen Code nur unter Windows 2000 SP4..... entweder mit Notepad (wenn es nur ein paar Zeilen sind) oder halt mit Notepad2.

Für tutorials.de nutze ich immer den IE 5.5.
Ansonsten, bei fremden Seiten den Netscape 7.1 oder Mozilla 1.7.5.
Netscape und Mozilla sind mir allerdings viel zu speicherhungrig..... daher nutze ich diese eigentlich auch nur bei Seiten die mir nicht so ganz geheuer sind.

Vom copy&paste wird es wohl nicht kommen, denn selbst wenn ich den Code hier komplett per Hand tippe, verschwinden die Formatierungen immer wenn ich auf "Vorschau" klicke.
 
Mal mit Mozilla und Vorschau testen.....
PHP:
<?php
    echo "MSN : $msn ";
    if($status=="online") {
        echo "<img src='msnon.png'>";
    } else {
        echo "<img src='msnoff.png'>";
    }
?>
[edit]
Hmm, scheint also am Browser zu liegen. *heul* :(
[/edit]
 
Zuletzt bearbeitet:
Moeglicherweise liegt es am IE5. Keine Ahnung. Muss mal schauen, hab glaub ich im Emulator noch ein Win2000 mit IE5 rumfliegen.
Nachtrag: Okay, kein 2000, ist ME, aber das sollte eigentlich egal sein, Hauptsache IE5.
 
Ja, scheint am IE5.5 zu liegen..... wie gesagt, den oben stehenden Code habe ich mal mit Mozilla und Vorschau gepostet und da klappt es.

Mit Mozilla kann ich hier aber kaum etwas lesen.
Ich muss die Schrift auf ca. 135% zoomen um ungefähr auf die Schriftgrösse vom IE5.5 (Schriftgrad mittel, also default) zu kommen.
Aber ständig hin und her zu zoomen, macht auch kein Spass.
 
Opera habe ich..... allerdings ausschliesslich um zu testen wie dort meine Seiten dargestellt werden.
Opera finde ich irgendwie..... nein, ich spreche es lieber nicht aus. ;)

Firefox wird wohl nicht viel ändern.
Im moment braucht der IE5.5 rund 60 MB Speicher, Mozilla liegt bei ca. 100 MB.
Da wird Firefox wohl nicht an den IE rankommen.
Soweit ich weiss, ist Firefox ja auch Mozilla gegenüber abgespeckt.

Kurzzeitig hatte ich mal den IE6 drauf gehabt, aber 1. hat dieser massive Probleme gemacht und 2. brauche ich ältere Browser auch zum testen meiner Seiten.
Schliesslich gehöre ich nicht zu den Leuten, die davon aus gehen das andere immer das neuste Sytsem haben.

[edit]
Ich habe 768 MB RAM, davon sind derzeit ca. 630 MB belegt.
Der letzte Reboot liegt zwar schon 14 Tage zurück, aber auch nach einem Reboot bin ich innerhalb weniger Stunden wieder bei min. über 500 MB.
Dürfte also verständlich sein dass ich möglichst auf speicherintensive Programme verzichten möchte. ;)
[/edit]

[edit2]
Ich meine mich ganz dunkel daran erinnern zu können, dass ich (zu Zeiten vom v5 Forum) irgendwo auch mal gelesen hatte, dass man min. den IE6 braucht.
Zumindest weiss ich jetzt aber dass es definitiv am Browser liegt und nicht an irgend welchen Tricks.
Vieleicht fällt mir ja noch irgendwas ein.
Ansonsten muss ich halt damit leben..... bzw. ihr auch, denn dieses Problem hält mich beim posten von Code natürlich immer etwas zurück.
Somit dürfte das Thema wohl erledigt sein.
[/edit2]
 
Zuletzt bearbeitet:
Dr Dau hat gesagt.:
[edit]
Ich habe 768 MB RAM, davon sind derzeit ca. 630 MB belegt.
Der letzte Reboot liegt zwar schon 14 Tage zurück, aber auch nach einem Reboot bin ich innerhalb weniger Stunden wieder bei min. über 500 MB.
Dürfte also verständlich sein dass ich möglichst auf speicherintensive Programme verzichten möchte. ;)
[/edit]
Hast du da schon den Cache/Buffer Speicher abgerechnet? Wenn ja solltest du evtl. einfach mal ein paar Programme die du nicht benötigst schliessen ;) Ansonsten ist ja nicht wirklich soviel Speicher belegt sondern wird durch den Cache/Buffer Speicher lediglich genutzt. Gegen das Darstellungsproblem mit der Schriftgröße solltest du im Firefox/Mozilla einfach die Mindestschriftgröße auf 11 oder 12 stellen.

Gruß
 
Ich habe nirgendwo etwas abgerechnet.
Nach einem Reboot liege ich bei um die 140 MB..... aber auch nur für gaaaaanz kurze Zeit. ;)
Ich brauch die Programme aber..... ansonsten kann ich den PC auch gleich aus lassen. :(

Mit der minimalen Schriftgrösse muss ich mir mal ansehen wenn ich wieder zu Hause bin..... hier sieht es jedenfalls schlecht aus (dadurch verschwinden einige Elemente Teilweise) *schei<piiiiep> DIV's* ;)
 
Zurück