[php] Regexy - tabela z wiki

Języki, których efekty są wykonywane po stronie serwera. PHP, ASP czy CGI w połączeniu z bazami danych SQL dają niezwykłe możliwości budowy i zarządzania stron WWW. Tu zadajemy pytania oraz demonstrujemy kod, z którym mamy problem.
pieto

[php] Regexy - tabela z wiki

Post autor: pieto »

Witam,
Wyciagnąlem sobie funkcje z wiki. ktora przerabia takie cuś np.:

{| border="1" cellpadding="2" cellspacing="0" align="right" width="300px" style="margin-left: 0.5em; margin-bottom: 0.5em" |+ Rumunia
România | align="center" colspan="2" style="background:#efefef;" | {| border="0" cellpadding="2" cellspacing="0" | align="center" width="160px" | Flaga Rumunii | align="center" width="140px" | Godło Rumunii |- | align="center" width="160px" | (o fladze) | align="center" width="140px" | (o godle) |} |---- | align="center" colspan="2" style="border-bottom:3px solid gray;" | Dewiza: brak |---- | align="center" colspan="2" | |---- | Język urzędowy | rumuński |---- | Stolica | Bukareszt |---- | Ustrój polityczny | demokracja parlamentarna |---- | Głowa państwa
| prezydent
Traian Băsescu |---- | Szef administracji
| premier
Calin Popescu-Tariceanu |---- | Powierzchnia
- całkowita
- wód śródląd. | 78.
238 391 km²
3,0% |---- | Liczba ludności
- ogółem (2003)
- gęstość zalud. | 49.
22 271 839
93,4/km² |---- | Uzyskanie niepodległości | od Imperium Osmańskiego
9 maja 1877 |---- | Waluta | Lej (ROL) |---- | Strefa czasowa | UTC +2 |---- | Hymn państwowy | ''Deşteaptă-te, Române!'' |---- | Domena internetowa | .ro |---- | Kod samochodowy | RO |---- | Numer kierunkowy | 40 |---- | align="center" colspan="2" style="border-bottom:3px solid gray;" | |}

na normalną tabelę..
funkcja wygląda tak:
[php]
function doTableStuff ( $t ) { // tabela
$fname = 'Parser::doTableStuff';
wfProfileIn( $fname );

$t = explode ( "\n" , $t ) ;
$td = array () ; # Is currently a td tag open?
$ltd = array () ; # Was it TD or TH?
$tr = array () ; # Is currently a tr tag open?
$ltr = array () ; # tr attributes
$indent_level = 0; # indent level of the table
foreach ( $t AS $k => $x )
{
$x = trim ( $x ) ;
$fc = substr ( $x , 0 , 1 ) ;
if ( preg_match( '/^(:*)\{\|(.*)$/', $x, $matches ) ) {
$indent_level = strlen( $matches[1] );
$t[$k] = "\n" .
str_repeat( '<dl><dd>', $indent_level ) .
'<table ' . $this->fixTagAttributes ( $matches[2] ) . '>' ;
array_push ( $td , false ) ;
array_push ( $ltd , '' ) ;
array_push ( $tr , false ) ;
array_push ( $ltr , '' ) ;
}
else if ( count ( $td ) == 0 ) { } # Don't do any of the following
else if ( '|}' == substr ( $x , 0 , 2 ) ) {
$z = "</table>" . substr ( $x , 2) . "\n";
$l = array_pop ( $ltd ) ;
if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
array_pop ( $ltr ) ;
$t[$k] = $z . str_repeat( '</dd></dl>', $indent_level );
}
else if ( '|-' == substr ( $x , 0 , 2 ) ) { # Allows for |---------------
$x = substr ( $x , 1 ) ;
while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
$z = '' ;
$l = array_pop ( $ltd ) ;
if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
array_pop ( $ltr ) ;
$t[$k] = $z ;
array_push ( $tr , false ) ;
array_push ( $td , false ) ;
array_push ( $ltd , '' ) ;
array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
}
else if ( '|' == $fc || '!' == $fc || '|+' == substr ( $x , 0 , 2 ) ) { # Caption
# $x is a table row
if ( '|+' == substr ( $x , 0 , 2 ) ) {
$fc = '+' ;
$x = substr ( $x , 1 ) ;
}
$after = substr ( $x , 1 ) ;
if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
$after = explode ( '||' , $after ) ;
$t[$k] = '' ;

# Loop through each table cell
foreach ( $after AS $theline )
{
$z = '' ;
if ( $fc != '+' )
{
$tra = array_pop ( $ltr ) ;
if ( !array_pop ( $tr ) ) $z = '<tr '.$tra.">\n" ;
array_push ( $tr , true ) ;
array_push ( $ltr , '' ) ;
}

$l = array_pop ( $ltd ) ;
if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
if ( $fc == '|' ) $l = 'td' ;
else if ( $fc == '!' ) $l = 'th' ;
else if ( $fc == '+' ) $l = 'caption' ;
else $l = '' ;
array_push ( $ltd , $l ) ;

# Cell parameters
$y = explode ( '|' , $theline , 2 ) ;
# Note that a '|' inside an invalid link should not
# be mistaken as delimiting cell parameters
if ( strpos( $y[0], '[[' ) !== false ) {
$y = array ($theline);
}
if ( count ( $y ) == 1 )
$y = "{$z}<{$l}>{$y[0]}" ;
else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
$t[$k] .= $y ;
array_push ( $td , true ) ;
}
}
}

# Closing open td, tr && table
while ( count ( $td ) > 0 )
{
if ( array_pop ( $td ) ) $t[] = '</td>' ;
if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
$t[] = '</table>' ;
}

$t = implode ( "\n" , $t ) ;
# $t = $this->removeHTMLtags( $t );
wfProfileOut( $fname );
return $t ;
}
[/php]

i mam takie pytanie jak by wykorzystac ten kod ?? tzn zeby mi zrobił to co trzeba w zmiennej $content ?? z góry dzięki, chyba że jest ktoś tak zdolny i ambitny by napisać własne regexy ??

z góry dzięki za pomoc
ODPOWIEDZ