Forums » Website ideas »
[DONE] Title conversion
Added by vl about 6 years ago
Currently, the title displayed in the profile is an identifier.
Could be nice to have an url like this:
http://www.example.com/title.php?title=Title00020&lang=en
that will return the human readable title in the specified language.
If someone do it, we'll be glad to integrate it in the official website.
Replies (5)
RE: Title conversion
-
Added by barbaros about 6 years ago
This could be faster with a database backend.
<?php
/* Requirements
*
* - title_words_de.txt, title_words_en.txt, title_words_fr.txt in the same directory
* - Multibyte Strings (mb_convert_encoding)
* - SimpleXML
**/
function rz_parse_title( $title, $lang = NULL ) {
$result = false;
// no title given
if ( empty($title) ) {
return $result;
}
// default language / strict check
if ( $lang === NULL ) {
$lang = 'en';
}
if ( !in_array($lang, array('de', 'en', 'fr')) ) {
return $result;
}
// parse the textfile
if ( $fp = @fopen('title_words_'. $lang .'.txt', 'r') ) {
while ( $line = fgets($fp) ) {
$line = mb_convert_encoding($line, 'UTF-8', 'UTF-16');
if ( preg_match('/\t('. $title .')\t(.*)\t(.*)$/i', $line, $matches) ) {
$result = $matches;
break;
}
}
fclose($fp);
}
return $result;
}
function rz_title_xml( $title, $lang = NULL ) {
$output = new SimpleXMLElement("<title />");
if ( $result = rz_parse_title($title, $lang) ) {
$output->addAttribute('id', $result[1]);
$output->addChild('male', $result[2]);
$output->addChild('female', $result[3]);
}
else {
$output->addChild('error');
}
echo $output->asXML();
}
// Output
rz_title_xml($_GET['title'], $_GET['lang']);
RE: Title conversion
-
Added by vl about 6 years ago
It will be even faster if you generate a .php file like:
title_fr.php that just contains a php array
RE: Title conversion
-
Added by barbaros about 6 years ago
Here we go.
<?php
/* Requirements
*
* - SimpleXML
*
* - For updates:
* - PHP5+ (file_put_contents)
* - Multibyte Strings (mb_convert_encoding)
* - title_words_de.txt, title_words_en.txt, title_words_fr.txt in the same directory
**/
// for update reasons
function rz_title_generate() {
$result = array();
$langs = array('de', 'en', 'fr');
foreach ( $langs as $lang ) {
if ( $fp = @fopen('title_words_'. $lang .'.txt', 'r') ) {
fgets($fp); // skip the header
while ( $line = fgets($fp) ) {
$line = mb_convert_encoding($line, 'UTF-8', 'UTF-16');
if ( preg_match('/\t(.*)\t(.*)\t([^\r\n]*)/i', $line, $matches) ) {
$title = strtolower($matches[1]);
$result[$title][$lang][0] = $matches[2];
$result[$title][$lang][1] = $matches[3];
}
}
fclose($fp);
}
}
file_put_contents('title_array.php', '<?php $title_array = '. var_export($result, true) .'; ?>');
}
function rz_title_xml( $title, $lang = NULL ) {
include_once('title_array.php');
if ( $lang === NULL ) {
$lang = 'en';
}
$title = strtolower($title);
$lang = strtolower($lang);
$output = new SimpleXMLElement("<title />");
if ( isset($title_array[$title][$lang]) ) {
$output->addAttribute('id', $title);
$output->addChild('male', $title_array[$title][$lang][0]);
$output->addChild('female', $title_array[$title][$lang][1]);
}
else {
$output->addChild('error');
}
echo $output->asXML();
}
// Update
//rz_title_generate();
// Output
header('Content-Type: text/plain');
rz_title_xml($_GET['title'], $_GET['lang']);
RE: Title conversion
-
Added by barbaros about 6 years ago
Oh, it was the debug file.
<?php
/* Requirements
*
* - SimpleXML
*
* - For updates:
* - PHP5+ (file_put_contents)
* - Multibyte Strings (mb_convert_encoding)
* - title_words_de.txt, title_words_en.txt, title_words_fr.txt in the same directory
**/
// for update reasons
function rz_title_generate() {
$result = array();
$langs = array('de', 'en', 'fr');
foreach ( $langs as $lang ) {
if ( $fp = @fopen('title_words_'. $lang .'.txt', 'r') ) {
fgets($fp); // skip the header
while ( $line = fgets($fp) ) {
$line = mb_convert_encoding($line, 'UTF-8', 'UTF-16');
if ( preg_match('/\t(.*)\t(.*)\t([^\r\n]*)/i', $line, $matches) ) {
$title = strtolower($matches[1]);
$result[$title][$lang][0] = $matches[2];
$result[$title][$lang][1] = $matches[3];
}
}
fclose($fp);
}
}
file_put_contents('title_array.php', '<?php $title_array = '. var_export($result, true) .'; ?>');
}
include_once('title_array.php');
function rz_title_xml( $title, $lang = NULL ) {
global $title_array;
if ( $lang === NULL ) {
$lang = 'en';
}
$title = strtolower($title);
$lang = strtolower($lang);
$output = new SimpleXMLElement("<title />");
if ( isset($title_array[$title][$lang]) ) {
$output->addAttribute('id', $title);
$output->addChild('male', $title_array[$title][$lang][0]);
$output->addChild('female', $title_array[$title][$lang][1]);
}
else {
$output->addChild('error');
}
echo $output->asXML();
}
// Update
//rz_title_generate();
// Output
rz_title_xml($_GET['title'], $_GET['lang']);
RE: [DONE] Title conversion
-
Added by vl about 6 years ago
(1-5/5)