Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在另一个字符串之前找到一个字符串_Php_String_Search - Fatal编程技术网

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

Php 如何在另一个字符串之前找到一个字符串,php,string,search,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-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.

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

?$Item_infobox=substr$Temp,strpos$Temp,{{Item infobox |+2,strpos$Temp,},strpos$Temp,{{Item infobox |+2-strpos$Temp{{Item infobox |-2;将'Item'更改为50种不同的类型。之后我想确定。现在我正在对字符串执行50次搜索。您提供给我们的文本中没有'Item infobox |'。请提供您的输入和预期输出,并将此注释合并到您的问题中。@LoneWolf您可以编辑您的注释将代码输入到您的问题中,以便其他人看到问题所在。我将在下班后编辑并添加代码。我还将提供一些必须使用的exmaple字符串。
//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";