douxue7196 2014-06-12 23:12
浏览 37
已采纳

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

I have below array:

[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)"

Now, simply I want this array as a result:

[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

I am using this code for getting the above results but still no lucks.

$match[0] contains the above array.

$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>";
        }
    }

Please can anybody rectify my code for the desired results.

  • 写回答

1条回答 默认 最新

  • dongsong8932 2014-06-12 23:19
    关注

    Use a regular expression to test the URLs for the pattern.

    // 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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料