创建该文本一个简单的PHP数组 [英] Create a simple PHP Array from this text

查看:96
本文介绍了创建该文本一个简单的PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML元素中找到的文本的这个可爱的块,并希望将其转换成一个简单的数组中,XML是wikia.com。

该数组将是这样的:

 卡名称=> [武术]战神,角色名=>战神,RELEASE_DATE => 2013年5月1日等等..

我已经试过各种内爆和爆炸的变化,但没有运气。这一个人难住我了。

  |卡名称= [武术]战神
|字符名称=战神
| RELEASE_DATE = 2013年5月1日
|图片1 = MartialAres5.jpg
|稀有1 =超级罕见的特
| PWR REQ 1 = 28
|销售价格1 = 94200
| MAX卡LV 1 = 60
| MAX掌握LV 1 = 40
|报价1 =希腊神话的时代期间使用战神prefers武器:剑,斧,和矛。但他可以使用任何武器熟练,并把最普通的物体变成致命的武器。
|基础攻击力1 = 2440
|基地DEF 1 = 2650
| MAX ATK 1 = 7015
|最大DEF 1 = 7613
|掌握奖金ATK 1 = 915
|掌握奖金DEF 1 = 993
|图片2 = MartialAres6.jpg
|稀有2 =终极罕见
|销售价格2 = 188400
| MAX掌握LV 2 = 200
|报价2 =下一次,我看到的大力士,我们将有一个钢制的谈话。它是关于时间让他回答我的屠杀斯廷法利斯湖怪鸟。
|最大攻击力2 = 9822
| MAX高清2 = 10660
|掌握奖金攻击力2 = 1098
|掌握奖金高清2 = 1192
|排列=卫兵
|能力=战鹰
|性别=男
|用法=平均
|派=超级英雄
|效果显着=您硬化的狂徒DEF。
| centretrait =无

code我试过:

 如果(file_exists('card.xml')){
    $ XML =使用simplexml_load_file('card.xml');
    $文字= $ XML的>页级> revision->文字;
    $ newtext =爆炸(|,$文字);
    的foreach($ newtext为newnewtext $){
        $ newtext2 =爆炸('=',$ newnewtext);
        的print_r($ newtext2);    }
}其他{
    退出(无法打开card.xml。');
}


解决方案

的要求:

 < PHP
$ file =文件|卡名称= [武术]战神
|字符名称=战神
| RELEASE_DATE = 2013年5月1日
|图片1 = MartialAres5.jpg
|稀有1 =超级罕见的特
| PWR REQ 1 = 28
|销售价格1 = 94200
| MAX卡LV 1 = 60
| MAX掌握LV 1 = 40
|报价1 =希腊神话的时代期间使用战神prefers武器:剑,斧,和矛。但他可以使用任何武器熟练,并把最普通的物体变成致命的武器。
|基础攻击力1 = 2440
|基地DEF 1 = 2650
| MAX ATK 1 = 7015
|最大DEF 1 = 7613
|掌握奖金ATK 1 = 915
|掌握奖金DEF 1 = 993
|图片2 = MartialAres6.jpg
|稀有2 =终极罕见
|销售价格2 = 188400
| MAX掌握LV 2 = 200
|报价2 =下一次,我看到的大力士,我们将有一个钢制的谈话。它是关于时间让他回答我的屠杀斯廷法利斯湖怪鸟。
|最大攻击力2 = 9822
| MAX高清2 = 10660
|掌握奖金攻击力2 = 1098
|掌握奖金高清2 = 1192
|排列=卫兵
|能力=战鹰
|性别=男
|用法=平均
|派=超级英雄
|效果显着=您硬化的狂徒DEF。
| centretrait =无;
$ X =爆炸(\\ n,$文件);
$ OUT =阵列();
的foreach($ x作为每个$){
  $ XX =爆炸('=',$个);
 $出[LTRIM($ XX [0],'|')] = $ XX [1];
}
呼应'< pre>';
的print_r($出);

工作演示: HTTP://$c$cpad.viper-7.com/3BkXD6

I have this lovely chunk of text found inside an XML element and want to convert it into a simple array, the XML is from wikia.com.

The array would be something like this:

card name => [Martial] Ares, character name => Ares, release_date => May 1 2013 and so on..

I've tried all sorts of imploding and exploding variations but no luck.. This one has got me stumped..

|card name=[Martial] Ares
|character name=Ares
|release_date=May 1 2013
|image 1=MartialAres5.jpg
|rarity 1=Super Special Rare
|pwr req 1=28
|sale price 1=94200
|max card lv 1=60
|max mastery lv 1=40
|quote 1=Ares prefers weapons that were used during the age of Greek myth: sword, axe, and spear.  But he can use any weapon expertly, and turn most ordinary objects into lethal weapons.
|base atk 1=2440
|base def 1=2650
|max atk 1=7015
|max def 1=7613
|mastery bonus atk 1=915
|mastery bonus def 1=993
|image 2=MartialAres6.jpg
|rarity 2=Ultimate Rare
|sale price 2=188400
|max mastery lv 2=200
|quote 2=Next time I see Hercules, We're going to have a steel conversation. It's about time for him to answer for massacring my Stymphalian Birds.
|max atk 2=9822
|max def 2=10660
|mastery bonus atk 2=1098
|mastery bonus def 2=1192
|alignment=Bruiser
|ability=Warhawk
|gender=Male
|usage=Average
|faction=Super Hero
|effect=Significantly harden DEF of your Bruisers.
|centretrait=None

Code I've tried:

if (file_exists('card.xml')) {
    $xml = simplexml_load_file('card.xml');


    $text = $xml->page->revision->text;
    $newtext = explode('|', $text);
    foreach($newtext as $newnewtext) {
        $newtext2 = explode('=', $newnewtext);
        print_r($newtext2);

    }


} else {
    exit('Failed to open card.xml.');
}

解决方案

as requested:

<?php
$file="|card name=[Martial] Ares
|character name=Ares
|release_date=May 1 2013
|image 1=MartialAres5.jpg
|rarity 1=Super Special Rare
|pwr req 1=28
|sale price 1=94200
|max card lv 1=60
|max mastery lv 1=40
|quote 1=Ares prefers weapons that were used during the age of Greek myth: sword, axe, and spear.  But he can use any weapon expertly, and turn most ordinary objects into lethal weapons.
|base atk 1=2440
|base def 1=2650
|max atk 1=7015
|max def 1=7613
|mastery bonus atk 1=915
|mastery bonus def 1=993
|image 2=MartialAres6.jpg
|rarity 2=Ultimate Rare
|sale price 2=188400
|max mastery lv 2=200
|quote 2=Next time I see Hercules, We're going to have a steel conversation. It's about time for him to answer for massacring my Stymphalian Birds.
|max atk 2=9822
|max def 2=10660
|mastery bonus atk 2=1098
|mastery bonus def 2=1192
|alignment=Bruiser
|ability=Warhawk
|gender=Male
|usage=Average
|faction=Super Hero
|effect=Significantly harden DEF of your Bruisers.
|centretrait=None";


$x=explode("\n",$file);
$out=array();
foreach($x as $each){
  $xx=explode('=',$each);
 $out[ltrim($xx[0],'|')]=$xx[1]; 
}
echo '<pre>';
print_r($out);

working demo: http://codepad.viper-7.com/3BkXD6

这篇关于创建该文本一个简单的PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆