• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[PHP] Some stupid convert thing :P

Colandus

Advanced OT User
Senator
Joined
Jun 6, 2007
Messages
2,434
Solutions
19
Reaction score
219
Location
Sweden
Heyy, was like 2+ months since I've really worked with PHP, I've lost my PHP brain xD

But w/e, I made this 'lil script :p
PHP:
<font face="verdana" style="font-size: 11px;">Paste your items.xml here!</font><br />
<form action="" method="POST" style="display: inline;">
	<textarea cols="120" rows="10" name="text" style="font: 10px verdana;"><?php /*<?php echo $_POST["text"]; ?>*/ ?></textarea><br />
	<font face="verdana" style="font-size: 11px;">Exceptions:</font>
	<input type="text" name="exceptions" value="a,an" style="font: 10px verdana;" /> <i>(separate with comma)</i><br />
	<input type="submit" value="Convert to Lua!" />
</form><br /><br />

<?php
$items_xml = $_POST["text"];
if ($items_xml) {
	preg_match_all("/\<item id=\"(\d+)\" article=\"(.*?)\" name=\".*\">/", $items_xml, $itemInfo);

	$itemList = array_combine($itemInfo[1], $itemInfo[2]);
	$exceptions = explode(",", $_POST["exceptions"]);
	foreach ($exceptions as $key => $exception) {
		$exceptions[$key] = trim($exception);
	}

	echo "<font face=\"verdana\" style=\"font-size: 11px;\">Copy your items.lua!</font><br />
	<textarea cols=\"120\" rows=\"10\" style=\"font: 10px verdana;\">itemArticles = {\n";
	foreach ($itemList as $itemID => $itemArticle) {
		if (!in_array($itemArticle, $exceptions)) {
			echo "\t[$itemID] = \"$itemArticle\",\n";
		}
	}
	echo "}</textarea>";
}

Not so usefull, but it creates like a Lua table like:
[itemid] = "article" :p

Hope it'll be used by sum1 lol xD
 
try this just remember i'm not good in php

PHP:
<?PHP
echo '<font face="verdana" style="font-size: 11px;">Paste your items.xml here!</font><br />';
echo '<form action="" method="POST" style="display: inline;">';
echo '<textarea cols="120" rows="10" name="text" style="font: 10px verdana;">';
echo '<$_POST["text"];></textarea><br />';
echo '<font face="verdana" style="font-size: 11px;">Exceptions:</font>';
echo '<input type="text" name="exceptions" value="a,an" style="font: 10px verdana;" /> <i>(separate with comma)</i><br />';
echo '<input type="submit" value="Convert to Lua!" />';
echo '</form><br /><br />';
?>

<?php
$items_xml = $_POST["text"];
if ($items_xml) {
    preg_match_all("/\<item id=\"(\d+)\" article=\"(.*?)\" name=\".*\">/", $items_xml, $itemInfo);

    $itemList = array_combine($itemInfo[1], $itemInfo[2]);
    $exceptions = explode(",", $_POST["exceptions"]);
    foreach ($exceptions as $key => $exception) {
        $exceptions[$key] = trim($exception);
    }

    echo "<font face=\"verdana\" style=\"font-size: 11px;\">Copy your items.lua!</font><br />
    <textarea cols=\"120\" rows=\"10\" style=\"font: 10px verdana;\">itemArticles = {\n";
    foreach ($itemList as $itemID => $itemArticle) {
        if (!in_array($itemArticle, $exceptions)) {
            echo "\t[$itemID] = \"$itemArticle\",\n";
        }
    }
    echo "}</textarea>";
}
?>


Some Tips:
WHy dont u creat an config file with all the information.

PHP:
include ('config.php');
echo '<title>'.$sitename.'</title>';

Now it will get the site name out of the config.php

PHP:
<?PHP
$sitename = "Title of the website";
?>
 
Last edited:
You only made it print the XML text there? I did that too first, but it comes some wierd error (not PHP error, XML error) idk why XD
 
Won't work, you should have written:
PHP:
echo $_POST["text"] . '</textarea><br />';

+ when I change to that, the error comes :(

error said:
Det går inte att visa XML-sidan
Det går inte att visa XML-indata med formatmallen XSL. Korrigera felet och klicka sedan på knappen Uppdatera eller försök igen senare.
--------------------------------------------------------------------------------

Bara ett element får finnas på högsta nivån i XML-dokumentet. Det gick inte att behandla resursen http://localhost/items.ph...

<font face="verdana" style="font-size: 11px;">Paste your items.xml here!</font><br /><form action="" me...
 
What extremely stupid way of doing this, preg is really slow.

PHP:
<?php
$file = '/home/anto/development/otserv/data/items/items.xml';

$xml = simplexml_load_file($file);

foreach($xml->item as $item) {
	echo 'item['. $item['id'] .'] =  "'. $item['name'] .'"';
}
?>
 
What extremely stupid way of doing this, preg is really slow.

PHP:
<?php
$file = '/home/anto/development/otserv/data/items/items.xml';

$xml = simplexml_load_file($file);

foreach($xml->item as $item) {
	echo 'item['. $item['id'] .'] =  "'. $item['name'] .'"';
}
?>

Lmao, I haven't been working with PHP for a while, forgot abit... But thanks for that one, actually haven't been working with simplexml before, but I think I get it now lol :D

Ty anto ;) ;) Could that also load it's attributes? :eek:
 
Back
Top