-2

我有一个字符串,例如:

"{{foo-1 456}}{{foo-2 abc}} {{foo-3 ghi}}{{foo-4 456}}{{foo-5 abc}}{{foo-6ghi}}"
              **                                      **
              find this position (1st)                not this position (2nd)

我需要找到第一个{{前面的foo(在这种情况下foo-2)前面的位置abc

foo内容和长度总是未知的,并且foo每次都可能不同,但我知道它在 a{{和之间,然后通过从该位置abc找到第一个从字符串中提取它。}}

结果应该是

"foo-2 abc"

我可以通过做很多单独的搜索来完成这个,但它会减慢我的脚本速度,因为这些字符串的大小可能会很大。

有没有办法从第一个开始向后搜索abc

这就是我现在使用的;它正在工作,但变大了。

//$Temp is found by the parameter $item in another routine. 
//but $Temp can change so its never the same.
//i just put it here like this so i dont have to put all the code.

$Temp = "__NOTOC__== Weak Potion of Centaur Slaying =={{Item infobox| name = Weak Potion of Centaur Slaying| icon = Weak Potion of Centaur Slaying.png| rarity = basic| description = Nourishment(30 m): +3% damage vs. centaur<BR>+10 Experience from kills| type = consumable| level = 5| value = 2}}{{Recipe| name = Weak Potion of Centaur Slaying| quantity = 5| mat-1 = Jug of Water| amt-1 =1| mat-2 = Rawhide Leather Scrap| amt-2 =1| mat-3 = Carrot| amt-3 =1| mat-4 = Pile of Glittering Dust| amt-4 =1| discipline = Artificer| level = 25}}{{clear}}== Minor Potion of Centaur Slaying =={{Item infobox| name = Minor Potion of Centaur Slaying| icon = Minor Potion of Centaur Slaying.png| rarity = basic| description = Nourishment(30 m): +5% damage vs. centaur<BR>+10 Experience from kills| type = consumable| level = 20| value = 2}}{{Recipe| name = Minor Potion of Centaur Slaying| quantity = 5| mat-1 = Jug of Water| amt-1 =1| mat-2 = Thin Leather Section | amt-2 =1| mat-3 = Carrot| amt-3 =1| mat-4 = Pile of Shimmering Dust| amt-4 =1| discipline = Artificer| level = 100}}{{clear}}== Potion of Centaur Slaying =={{Item infobox| name = Potion of Centaur Slaying| icon = Potion of Centaur Slaying.png| rarity = basic| description = Nourishment (30 m): 7% damage vs centaur<BR>-4% damage from centaur<BR>+10 Experience from kills| type = consumable| level = 35| value = 2}}{{Recipe| name = Potion of Centaur Slaying| quantity = 5| mat-1 = Jug of Water | amt-1 =1| mat-2 = Coarse Leather Section| amt-2 =1| mat-3 = Carrot| amt-3 =1| mat-4 = Pile of Radiant Dust| amt-4 =1| discipline = Artificer| level = 175}}{{clear}}== Strong Potion of Centaur Slaying =={{Item infobox| name = Strong Potion of Centaur Slaying| icon = Strong Potion of Centaur Slaying.png| rarity = basic| description = Nourishment (30 m): 8% damage vs centaur<BR>-6% damage from centaur<BR>+10 Experience from kills | type = consumable| level = 50| value = 2}}{{Recipe| name = Strong Potion of Centaur Slaying| quantity = 5| mat-1 = Jug of Water| amt-1 =1| mat-2 = Rugged Leather Section | amt-2 = 3 Carrot| amt-3 = 1| mat-4 = Pie of Luminous Dust| amt-4 = 1| discipline = Artificer| level = 250}}{{clear}}== Potent Potion of Centaur Slaying =={{Item infobox| name = Potent Potion of Centaur Slaying| icon = Potent Potion of Centaur Slaying.png| rarity = basic| description = Nourishment (30 m): 9% damage vs centaur<BR>-7% damage from centaur<BR>+10 Experience from kills | type = consumable| level = 65| value = 2}}{{Recipe| name = Potent Potion of Centaur Slaying| quantity = 5| mat-1 = Jug of Water| amt-1 =1| mat-2 = Thick Leather Section| amt-2 =1| mat-3 = Carrot| amt-3 =1| mat-4 = Pile of Incandescent Dust| amt-4 =1| discipline = Artificer| level = 325}}{{clear}}== Powerful Potion of Centaur Slaying =={{Item infobox| name = Powerful Potion of Centaur Slaying| icon = Powerful Potion of Centaur Slaying.png| rarity = basic| description = Nourishment (1 h): 10% damage vs centaur<BR>-10% damage from centaur<BR>+10 Experience from kills | type = consumable| level = 80| value = 2}}{{Recipe| name = Powerful Potion of Centaur Slaying| quantity = 10| mat-1 = Jug of Water | amt-1 =1| mat-2 = Hardened Leather Section | amt-2 =1| mat-3 = Carrot| amt-3 =1| mat-4 = Pile of Crystalline Dust| amt-4 =1 | discipline = Artificer| level = 400}}{{clear}}== Notes ==*== Trivia ==*== Bugs ==* Currently, entering the recipe for the Powerful variant of the potion in the discovery pane twice will discover the recipe for the Extended variant of the potion. These potions are identical to Powerful potions, only of Rare quality, soulbound, and worth 1c";
$Item = "Minor Potion of Centaur Slaying";

// start : is there an item infobox this time? (for the specific item)
if(stristr($Temp, '{{Item infobox |') !== FALSE)
{
    // check to see if its the one i want
    do
    {
        $Item_infobox = substr($Temp, strpos($Temp, "{{Item infobox |") + 2, strpos($Temp, "}}",strpos($Temp, "{{Item infobox |") + 2) - strpos($Temp, "{{Item infobox |") - 2);
        $Temp = str_replace("{{" . $Item_infobox . "}}", "", $Temp);
        if(stristr($Item_infobox, '| name = ' . $Item) !== FALSE)
        {
            // found it
            echo $Item_infobox . "<br/><br/>";  
            break;  
        }
    } 
    while (stristr($Temp, '{{Item infobox |') !== FALSE);
}
// stop : is it an item infobox this time? (for the specific item)


// start : is there an crafting infobox this time? (for the specific item)
if(stristr($Temp, '{{Crafting infobox |') !== FALSE)
{
    // check to see if its the one i want
    do
    {
        $Crafting_infobox = substr($Temp, strpos($Temp, "{{Crafting infobox |") + 2, strpos($Temp, "}}",strpos($Temp, "{{Crafting infobox |") + 2) - strpos($Temp, "{{Crafting infobox |") - 2);
        $Temp = str_replace("{{" . $Crafting_infobox . "}}", "", $Temp);
        if(stristr($Crafting_infobox, '| name = ' . $Item) !== FALSE)
        {
            // found it
            echo $Crafting_infobox . "<br/><br/>";  
            break;  
        }
    } 
    while (stristr($Temp, '{{Crafting infobox |') !== FALSE);
}
// stop : is it an crafting infobox this time? (for the specific item)

// repeat this routine for every different infobox (around 50) until i find the correct one.

这里有一些其他示例 $Temp 和 $Item 属于一起。再次。我每次都必须根据 $item 获得 $temp,因为 $temp 我一定会随着时间的推移而改变。这只是为了展示字符串的多样性。

//exammple 2
$Temp = "{{stub}}{{Crafting infobox| name = Gift of Color| type = legendary| description = A gift of color used to create [[The Bifrost]].| rarity = legendary }}==Acquisition==A [[Gift of Color]] can be crafted by a level 400 [[Cook]]. The required [[Recipe: Gift of Color]] can be bought from [[Miyani]] at the [[Mystic Forge]].{{Recipe | name = Gift of Color| mat-1 = Pile of Crystalline Dust| amt-1 = 250| mat-2 = Opal Orb| amt-2 = 100| mat-3 = Gift of Zhaitan| amt-3 = 1| mat-4 = Unidentified Dye| amt-4 = 250|discipline=Chef|level=400|exp}}";
$Item = "Gift of Color";

//exammple 3
$Temp = "{{Inventory infobox| name = 20 Slot Safe Box| slots= 20| property =| description = 20 Slots, Items in this box will never appear in a sell-to-vendor list and will not move when inventory is sorted.| rarity = fine| type = box| value = 54}}{{clear}}== Recipes  =={{Recipe| name = 20 Slot Safe Box| mat-1 = Orichalcum Ingot| amt-1 = 10| mat-2 = Superior Rune of Holding| amt-2 = 1| mat-3 = Pile of Crystalline Dust| amt-3 = 3| discipline = Armorsmith| level = 400}}==See also==*[[20 Slot Invisible Bag]]*[[20 Slot Invisible Leather Pack]][[Category:Armorsmith recipes]]";
$Item = "20 Slot Safe Box";
4

1 回答 1

0

在 substr 中使用 preg_replace。我认为这会有所帮助。

于 2013-01-14T15:41:52.227 回答