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

时间:2014-06-12 23:12:37

标签: 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&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>";
        }
    }

任何人都可以纠正我的代码以获得理想的结果。

1 个答案:

答案 0 :(得分:2)

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

// 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);
}