Benutzer:DerFussi/VCards extrahieren

Aus Wikivoyage
# Extracting the vcards/listings from the article

# I think you take it from the url
$lang = $_GET['lang']; 

# getting the article text
$content = file_get_contents("http://" . $lang . ".wikivoyage.org/w/index.php?title=" . $file . "&action=raw");

# defining the names of the templates for each language Version
$vcard['de'] = 'vcard';
$vcard['en'] = 'go|see|do|buy|eat|drink|sleep';

# defining the options name: e.g. the latitude
$lat['en'] = 'lat';
$lat['de'] = 'lat';

# extracting all vcards
# it searches case-insensitive, may not work properly with non Latin characters
preg_match_all ( "/\{\{({$vcard[$lang]}).*?(.*?\{\{.*?\}\})*.*?\}\}/i", $content, $matches, PREG_PATTERN_ORDER );

# $matches[0][0] contains the first listing
# $matches[0][1] contains the second listing
# $matches[1][0] contains the template's name of the first listing
# $matches[1][1] contains the template's  name of the second listing

# just go through all listings now
foreach ( $matches[0] as $listing ):

  # Splitting up the listings (splits everything, even if therer are other templates included. 
  # can be a Problem, if the included templates have the same named options
  $options = explode ( '|', $listing );

  # going through the Options and processing the options as you want
  foreach ( $options as $entry ):

    # e.g. checking for latitude and do something
    if ( strpos ( ltrim ( $entry ), $lat[$lang] ) === 0 ):
      $vcard_option = explode ( '=', $entry );
      $latitude = (float) trim ( $vcard_option[1] );
    endif:

    # here you can take care of the other options

  endforeach;

endforeach;