Skip to content

Instantly share code, notes, and snippets.

@sahyung
Created March 18, 2018 15:37
Show Gist options
  • Select an option

  • Save sahyung/0e43ce2efeb56f938c28b8b89eef72e1 to your computer and use it in GitHub Desktop.

Select an option

Save sahyung/0e43ce2efeb56f938c28b8b89eef72e1 to your computer and use it in GitHub Desktop.
GALAXY MERCHANT TRADING GUIDE
<html>
<body>
<form action="" method="post">
<textarea name="input" rows="10" cols="80" placeholder="
example:
how much is pish tegj glob glob ?
how many Credits is glob prok Silver ?
how many Credits is glob prok Gold ?
how many Credits is glob prok Iron ? "></textarea><br>
<button name="convert" type="submit" value="CONVERT">CONVERT</button>
</form>
</body>
</html>
<?php
/**
* convert a romanian text to an arabic number
* @param string romanian text
* @return integer arabic
*/
function toArabic($text)
{
$romanian = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]; // array containing const romanian number
$arabic = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]; // array containing associated number romanian
for ($i = 0; $i < count($romanian); $i++) // Loop through romanian
{
if(strpos($text, $romanian[$i]) === 0) // check if element of romanian is on first occurence
{
return $arabic[$i] + toArabic(replaceFirst($romanian[$i], '',$text)); //rinse and repeat conversion
}
}
return 0; // return on invalid input or end of conversion
}
/**
* convert a galactic text to romanian
* @param string galactic text
* @return string romanian
*/
function toRomanian($text)
{
$romanian = ["L", "XL", "X", "IX", "V", "IV", "I"]; // array containing const romanian number
$galactic = ["tegj", "pishtegj", "pish", "globpish", "prok", "globprok", "glob"]; // array containing const galactic number
$text = trim($text);
for ($i = 0; $i < count($galactic); $i++) // Loop through romanian
{
if(strpos($text, $galactic[$i]) === 0) // check if element of galactic is on first occurence
{
return $romanian[$i] . toRomanian(replaceFirst($galactic[$i], '',$text)); //rinse and repeat conversion
}
}
return '';
}
function replaceFirst($from, $to, $content)
{
$from = '/'.preg_quote($from, '/').'/';
return preg_replace($from, $to, $content, 1);
}
function get_string_between($array, $start, $end){
$array = ' ' . $array;
$ini = strpos($array, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($array, $end, $ini) - $ini;
return substr($array, $ini, $len);
}
if(isset($_POST['convert']))
{
$input = get_string_between($_POST['input'], "is", "?"); //only get text between 'is' and '?'
$result = toArabic(toRomanian($input)); //convert
if($result === 0)
{
echo "I have no idea what you are talking about";
exit;
}
if(stripos($input, 'Silver') !== false)
{
$result = $result*17;
}elseif (stripos($input, 'Gold') !== false) {
$result = $result*14450;
}elseif (stripos($input, 'Iron') !== false) {
$result = $result*391/2;
}
echo $input . " is " . $result . " Credits";
exit;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment