Seite 1 von 1

PHP: Parser parst nicht

Verfasst: 24.10.2002 18:57:31
von feltel
Ich hab für die Newsseite mir ne kleine Parserfunktion zusammengestückelt (aus dem phpBB2-Code, includes/bbcode.php), die URLs in HTML-Formatierte Links umwandeln soll. Nur leider funktioniert die nur zur hälfte.

Code: Alles auswählen

function make_clickable($text)
{

	// pad it with a space so we can match things at the start of the 1st line.
	$ret = " " . $text;

	// matches an "xxxx://yyyy" URL at the start of a line, or after a space.
	// xxxx can only be alpha characters.
	// yyyy is anything up to the first space, newline, or comma.
	$ret = preg_replace("#([\n ])([a-z]+?)://([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]+)#i", "\\1<a href=\"\\2://\\3\" target=\"_blank\">\\2://\\3</a>", $ret);

	// matches a "www.xxxx.yyyy[/zzzz]" kinda lazy URL thing
	// Must contain at least 2 dots. xxxx contains either alphanum, or "-"
	// yyyy contains either alphanum, "-", or "."
	// zzzz is optional.. will contain everything up to the first space, newline, or comma.
	// This is slightly restrictive - it's not going to match stuff like "forums.foo.com"
	// This is to keep it from getting annoying and matching stuff that's not meant to be a link.
	$ret = preg_replace("#([\n ])www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]*)?)#i", "\\1<a href=\"http://www.\\2.\\3\\4\" target=\"_blank\">www.\\2.\\3\\4</a>", $ret);

	// matches an email@domain type address at the start of a line, or after a space.
	// Note: Only the followed chars are valid; alphanums, "-", "_" and or ".".
	$ret = preg_replace("#([\n ])([a-z0-9\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)?[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);

	// Remove our padding..
	$ret = substr($ret, 1);

	return($ret);
}
Wenn ich die Funktion direkt mit nem Wert aufrufe, dann bekomme ich als Ausgabe nen korrekt HTML-formatierten Link. Wenn ich die Funktion aber mit ner Variable aufrufe, deren Inhalt aus ner Textdatei eingelesen wurde, dann bekomm ich nur den Variableninhalt zurück.

Code: Alles auswählen

echo (make_clickable("dies ist ein Link auf http://www.feltel.de"));
und

Code: Alles auswählen

$msg = "dies ist ein Link auf http://www.feltel.de";
echo (make_clickable($msg));
klappen einwandfrei, während

Code: Alles auswählen

$msg = readfile('irgendeintextfile');
echo (make_clickable($msg));
nicht das gewünschte Ergebniss bringt. Wo klemmts? Hab schon ne andere Textdatei als Quelle genommen, das funktioniert aber genausowenig.

Verfasst: 24.10.2002 19:28:09
von Six
Als erstes, siehe hier: http://www.debianforum.de/forum/viewtopic.php?t=3004 ;-)

Als zweites: k A.

Verfasst: 24.10.2002 19:30:23
von feltel
Six hat geschrieben:Als erstes, siehe hier: http://www.debianforum.de/forum/viewtopic.php?t=3004 ;-)
grrr
Als zweites: k A.
grrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr

:mrgreen:

Verfasst: 24.10.2002 19:42:50
von abi
hast du mal probiert das teil z.b. über

Code: Alles auswählen

$file = file('diedatei');

foreach($file as $line) {
   echo (make_clickable($line));
}

auszugeben ?

ach ja: http://www.debianforum.de/forum/viewtopic.php?t=3004

Verfasst: 24.10.2002 19:43:56
von pdreker
Laut meinem O'Reilly "Programming PHP" macht readfile folgendes: Es gibt die eingelesen Datei sofort aus, und der Rückgabewert ist die Anzahl gelesener Bytes.

Dein

Code: Alles auswählen

$msg = readfile('irgendeintextfile'); 
echo (make_clickable($msg));
gibt also schon in der ersten Zeile die Datei aus, und $msg ist danach dann 1234 (die Anzahl Bytes der gelesenen Datei.

Du musst die file() oder fread() Funktion benutzen, um ein Array oder einen String aus einem File zu machen.

Sinnvoller wäre es aber wahrscheinlich, die News als TEXT oder BLOB in der Database zu speichern...

Patrick

Verfasst: 25.10.2002 07:13:02
von feltel
try hat geschrieben:hast du mal probiert das teil z.b. über ...
das funzt :P