想請問各位高手 替換字元有沒有更好的做法

以下是我的php
<?php

	$DB_locate = "localhost";
	$DB_name = "minecraft";
	$DB_password = "TmXBYLUCd0PHiEWH";

	$link=mysqli_connect($DB_locate,$DB_name,$DB_password)or die("連接失敗");
	mysqli_set_charset($link, "UTF8");
	mysqli_select_db($link, "minecraft_database");
	
	$sql = "SELECT * FROM `webshop` ORDER BY `id` DESC";
	$result = mysqli_query($link,$sql);
	
	$i = 1;
	while($row = mysqli_fetch_assoc($result)){
		
		$meta = str_replace("UNSPECIFIC_META:", "", $row['meta']);
		$meta2 = str_replace("ENCHANTED_META:", "", $meta);
		$meta3 = str_replace("{meta-type=UNSPECIFIC, ", "", $meta2);
		$meta4 = str_replace("{meta-type=ENCHANTED, stored-enchants={", "", $meta3);
		$meta5 = str_replace("enchants={", "", $meta4);
		$meta6 = str_replace("}", "", $meta5);
		$meta7 = str_replace(", ", "&nbsp;&nbsp;&nbsp;&nbsp;", $meta6);
		$meta8 = str_replace("Damage", "耗損", $meta7);
		$meta9 = str_replace("DURABILITY", "耐久", $meta8);
		$meta10 = str_replace("DAMAGE_ALL", "鋒利", $meta9);
		$meta11 = str_replace("PROTECTION_ENVIRONMENTAL", "保護", $meta10);
		$meta12 = str_replace("PROTECTION_FIRE", "火焰保護", $meta11);
		$meta13 = str_replace("PROTECTION_FALL", "輕盈", $meta12);
		$meta14 = str_replace("PROTECTION_EXPLOSIONS", "爆炸保護", $meta13);
		$meta15 = str_replace("PROTECTION_PROJECTILE", "投射性保護", $meta14);
		$meta16 = str_replace("OXYGEN", "水中呼吸", $meta15);
		$meta17 = str_replace("WATER_WORKER", "親水性", $meta16);
		$meta18 = str_replace("THORNS", "尖刺", $meta17);
		$meta19 = str_replace("DEPTH_STRIDER", "深海漫遊", $meta18);
		$meta20 = str_replace("FROST_WALKER", "冰霜行者", $meta19);
		$meta21 = str_replace("BINDING_CURSE", "綁定詛咒", $meta20);
		$meta22 = str_replace("DAMAGE_UNDEAD", "不死剋星", $meta21);
		$meta23 = str_replace("DAMAGE_ARTHROPODS", "節肢剋星", $meta22);
		$meta24 = str_replace("KNOCKBACK", "擊退", $meta23);
		$meta25 = str_replace("FIRE_ASPECT", "燃燒", $meta24);
		$meta26 = str_replace("LOOT_BONUS_MOBS", "掠奪", $meta25);
		$meta27 = str_replace("SWEEPING_EDGE", "橫掃之刃", $meta26);
		$meta28 = str_replace("ARROW_DAMAGE", "強力", $meta27);
		$meta29 = str_replace("ARROW_擊退", "衝擊", $meta28);
		$meta30 = str_replace("ARROW_FIRE", "火焰", $meta29);
		$meta31 = str_replace("ARROW_INFINITE", "無限", $meta30);
		$meta32 = str_replace("LOYALTY", "忠誠", $meta31);
		$meta33 = str_replace("IMPALING", "魚叉", $meta32);
		$meta34 = str_replace("RIPTIDE", "波濤", $meta33);
		$meta35 = str_replace("CHANNELING", "喚雷", $meta34);
		$meta36 = str_replace("MENDING", "修補", $meta35);
		$meta37 = str_replace("VANISHING_CURSE", "消失詛咒", $meta36);
		$meta38 = str_replace("DIG_SPEED", "效率", $meta37);
		$meta39 = str_replace("SILK_TOUCH", "絲綢之觸", $meta38);
		$meta40 = str_replace("LOOT_BONUS_BLOCKS", "幸運", $meta39);
		$meta41 = str_replace("LUCK", "海洋的祝福", $meta40);
		$meta42 = str_replace("LURE", "魚餌", $meta41);
		$meta43 = str_replace("POTION_META:{meta-type=POTION", "", $meta42);
		$meta44 = str_replace("&nbsp;&nbsp;&nbsp;&nbsp;potion-type=", "", $meta43);
		$meta45 = str_replace("minecraft:water", "水瓶", $meta44);
		$meta46 = str_replace("minecraft:mundane", "平凡藥水", $meta45);
		$meta47 = str_replace("minecraft:thick", "黏稠藥水", $meta46);
		$meta48 = str_replace("minecraft:awkward", "基礎藥水", $meta47);
		$meta49 = str_replace("minecraft:night_vision", "夜視藥水(3:00)", $meta48);
		
		if($i < 10){
			echo '0'.
				 $i.'&nbsp;&nbsp;&nbsp;&nbsp;'.
				 $row['title'].'&nbsp;&nbsp;&nbsp;&nbsp;'.
				 $row['amount'].'&nbsp;&nbsp;&nbsp;&nbsp;'.
				 $row['price'].'元&nbsp;&nbsp;&nbsp;&nbsp;'.
				 $meta49.
				 '<br><br>';
		}else{
			echo 
				 $i."&nbsp;&nbsp;&nbsp;&nbsp;".
				 $row['title'].'&nbsp;&nbsp;&nbsp;&nbsp;'.
				 $row['amount'].'&nbsp;&nbsp;&nbsp;&nbsp;'.
				 $row['price'].'元&nbsp;&nbsp;&nbsp;&nbsp;'.
				 $meta49.
				 '<br><br>';
		}
		
		$i++;
	}
	
	mysqli_close($link)or die("關閉失敗");
?>

我從資料庫抓取資料後需要對這個資料後製成指定的格式
這些資料並不是一模一樣 但是他有一定的規則
想請問各位高手 是不是除了我這樣做有更好的做法?
標籤:

評論

  • 如果你的需求是要大量把A的資料取代為B,或許你能用另一個方式來做:

    寫一個array宣告為全域變數,或者存成一個php檔案,需要時include就可使用,這個好處是,你只要維護這個檔案就好,其他程式都用include來使用

    一個簡單的例子給你參考
    # 這段是轉換array
    $transfer_array = array(
    	'Damage' => '耗損',
    	'DURABILITY' => '耐久',
    	'DAMAGE_ALL' => '鋒利',
    );
    
    # 這段是如何使用
    $transfer_data = $row['meta']; # 假設已經拿到你要轉換的字串
    foreach ($transfer_array as $k => $v){ # 批次讀取array
    	$transfer_data = str_replace($k, $v, $transfer_data); # 批次把array的key取代為value
    }
    echo $transfer_data; # 所有你要轉換的數值都做完了
    
Sign In or Register to comment.