如何使用PHP获取基于字符串起点和终点的特定信息

如何使用PHP获取基于字符串起点和终点的特定信息,php,Php,我有以下资料: [0] => href="/favicon.ico" [1] => href="/w/opensearch_desc.php" [2] => href="http://www.xxxxx.org/w/api.php?action=rsd" [3] => href="http://www.gnu.org/copyleft/fdl.html" [4] => href="/w/index.php?title=Special:RecentChanges&am

我有以下资料:

[0] => href="/favicon.ico"
[1] => href="/w/opensearch_desc.php"
[2] => href="http://www.xxxxx.org/w/api.php?action=rsd"
[3] => href="http://www.gnu.org/copyleft/fdl.html"
[4] => href="/w/index.php?title=Special:RecentChanges&feed=atom"
[5] => href="/w/skins/common/commonPrint.css"
[6] => href="#navigation"
[7] => href="#p-wikisearch"
[8] => href="/wiki/Bookshelf_How-To"
[9] => href="/wiki/Category:DE_B%C3%BCcherregal"
[10] => href="/wiki/Category:FR_Genre"
[11] => href="/wiki/Category:IT_Biblioteca"
[12] => href="/wiki/Category:PT_Prateleira"
[13] => href="/wiki/Category:Agriculture_Bookshelf"
[14] => href="/wiki/Category:Animal"
[15] => href="/wiki/Category:Children%27s_Bookshelf"
[16] => href="/wiki/Category:Classics_Bookshelf"
[17] => href="/wiki/Category:Countries_Bookshelf"
[18] => href="/wiki/Category:Crime_Bookshelf"
[19] => href="/wiki/Category:Education_Bookshelf"
[20] => href="/wiki/Category:Fine_arts_Bookshelf"
[21] => href="/wiki/Category:General_Works_Bookshelf"
[22] => href="/wiki/Category:Geography_Bookshelf"
[23] => href="/wiki/Category:Health_Bookshelf"
[24] => href="/wiki/Category:History_Bookshelf"
[25] => href="/wiki/Category:Language_and_Literature_Bookshelf"
[26] => href="/wiki/Category:Law_Bookshelf"
[27] => href="/wiki/Category:Library_Science"
[28] => href="/wiki/Category:Music_Bookshelf"
[29] => href="/wiki/Category:Periodicals_Bookshelf"
[30] => href="/wiki/Category:Political_Science_Bookshelf"
[31] => href="/wiki/Category:Psychology_and_Philosophy_Bookshelf"
[32] => href="/wiki/Category:Religion_Bookshelf"
[33] => href="/wiki/Category:Science_Bookshelf"
[34] => href="/wiki/Category:Social_Sciences_Bookshelf"
[35] => href="/wiki/Category:Technology_Bookshelf"
[36] => href="/wiki/Category:Wars_Bookshelf"
[37] => href="/w/index.php?title=Category:Bookshelf&pagefrom=The+Galaxy+%28Bookshelf%29#mw-pages"
[38] => href="/wiki/Adventure_(Bookshelf)"
[39] => href="/wiki/Africa_(Bookshelf)"
[40] => href="/wiki/African_American_Writers_(Bookshelf)"
现在,我只想得到这个数组:

[13] => Agriculture_Bookshelf
[14] => Animal
[15] => Children%27s_Bookshelf
[16] => Classics_Bookshelf
[17] => Countries_Bookshelf
[18] => Crime_Bookshelf
[19] => Education_Bookshelf
[20] => Fine_arts_Bookshelf
[21] => General_Works_Bookshelf
[22] => Geography_Bookshelf
[23] => Health_Bookshelf
[24] => History_Bookshelf
[25] => Language_and_Literature_Bookshelf
[26] => Law_Bookshelf
[27] => Library_Science
[28] => Music_Bookshelf
[29] => Periodicals_Bookshelf
[30] => Political_Science_Bookshelf
[31] => Psychology_and_Philosophy_Bookshelf
[32] => Religion_Bookshelf
[33] => Science_Bookshelf
[34] => Social_Sciences_Bookshelf
[35] => Technology_Bookshelf
[36] => Wars_Bookshelf
我正在使用这段代码获得上述结果,但仍然没有运气

$match[0]
包含上述数组

$str = '_Bookshelf';  
if(in_array($str,$match[0])){
    echo "FOUND!!";
    foreach($match[0] as $asd=>$asds){
        echo "<pre>";echo str_replace('href="/wiki/Category:','',$asds);echo "</pre>";
        }
    }
$str=''u书架';
if(在数组中($str,$match[0])){
回声“找到了!!”;
foreach($asd=>$asd时匹配[0]{
echo”“;echo str_replace('href=“/wiki/Category:”,''$asds);echo”“;
}
}

请修改我的代码以获得所需的结果。

使用正则表达式测试模式的URL


in_array()
查找精确匹配,而不是子字符串,因此第一个
if
将无法成功使用上述数组。谢谢,请您更正所需结果。第一类代码,第一类。但我在最后一个中有
类似:
战争书架“类别”书架第二:我不想要这些:
DE_B%C3%BCcherregal“FR_体裁”IT_Biblioteca“PT_pratelera”
Categories“Bookshelf”Categories“DE_B%C3%BCcherregal”FR_体裁“IT_Biblioteca”PT_pratelera“
我没有注意到值中的引号(我以为他们只是PHP引用了
var_dump
output中的字符串),我修复了regexp以返回结束引号之前的部分。它不会返回
DE_B%C3%BCcherregal
,因为它不会以
\u Bookshelf结尾。”
。您好,您注意到了吗,您在数组中遗漏了
动物
图书馆学
?请重新检查您的代码。我也需要它们在数组中,因为我的问题是要包括开始
\u书架
和结束
\u书架
中的所有内容。只需将
\u书架之间的所有类别包含在内e> 作为开始,作为结束。我指的是一种范围。类似的。
// Find the first _Bookshelf element
foreach ($match[0] as $key => $value) {
    if (preg_match('#/wiki/Category:(.*_Bookshelf)"$#', $value)) {
        $first_index = $key;
        break;
    }
}
// Find the last _Bookshelf element by searching the reversed array
foreach (array_reverse($match[0]) as $key => $value) {
    if (preg_match('#/wiki/Category:(.*_Bookshelf)"$#', $value)) {
        $last_index = $key;
        break;
    }
}
// Get the selected part of the array
$result = array_slice($match[0], $first_index, -$last_index);
// Remove the URL prefix and quote at the end
foreach ($result as &$val) {
    $val = preg_replace('#.*/wiki/Category:(.*)"$#', '$1', $val);
}