如何在另一个字符串之前找到一个字符串

时间:2013-01-14 04:43:03

标签: php string search

我有一个字符串例如:

"{{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每次都可能不同,但我知道它在{{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";

1 个答案:

答案 0 :(得分:0)

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