duanluanlang8501 2017-08-24 18:04
浏览 585
已采纳

PHP在foreach循环中替换多个字符串

I'm grabbing the source of a page with a lot of PDF links (files) listed in a column like so. simple_php_dom.php

TALB20170826D-$A$$-RA11.pdf
TAP$20170826D-$A$$-RA11.pdf
TASD20170826D-$A$$-RA11.pdf
TAUA20170826D-$A$$-RA11.pdf
TAUB20170826D-$A$$-RA11.pdf
TAUC20170826D-$A$$-RA11.pdf
TAUD20170826D-$A$$-RA11.pdf
TBTP20170826D-$A$$-RA11.pdf
TCBY20170826D-$A$$-RA11.pdf

I need to rename them within the foreach loop

foreach($html->find('a') as $element) 
echo $element->href;

For instance TALB is an abbreviation for ALBANY, TAP is an abbreviation for Asia Pacific and so on.

I have a list of the names that correspond with the abbreviations but unsure how to rename them within the loop? Any help would be greatly appreciated!

  • 写回答

1条回答 默认 最新

  • dqkv0603 2017-08-24 18:08
    关注

    You can put the list of replacements in arrays:

    <?php    
    /*
    $list = [];   
    $list[] = "TALB20170826D-\$A$$-RA11.pdf";
    $list[] = "TAP$20170826D-\$A$$-RA11.pdf";
    $list[] = "TASD20170826D-\$A$$-RA11.pdf";
    $list[] = "TAUA20170826D-\$A$$-RA11.pdf";
    $list[] = "TAUB20170826D-\$A$$-RA11.pdf";
    $list[] = "TAUC20170826D-\$A$$-RA11.pdf";
    $list[] = "TAUD20170826D-\$A$$-RA11.pdf";
    $list[] = "TBTP20170826D-\$A$$-RA11.pdf";
    $list[] = "TCBY20170826D-\$A$$-RA11.pdf";
    */
    $list = $html->find('a');
    $abbr = [
        "TALB",
        "TAP",
        // ...
    ];
    $replacements = [
        "ALBANY",
        "Asia Pacific",
        // ...
    ];
    foreach ($list as &$el) {
        $el->href = str_replace($abbr, $replacements, $el->href);
    }
    

    Demo

    Or, to keep them all in one associative array (order doesn't matter and missing items just won't be replaced, no errors):

    <?php    
    /*$list = [];   
    $list[] = "TALB20170826D-\$A$$-RA11.pdf";
    $list[] = "TAP$20170826D-\$A$$-RA11.pdf";
    $list[] = "TASD20170826D-\$A$$-RA11.pdf";
    $list[] = "TAUA20170826D-\$A$$-RA11.pdf";
    $list[] = "TAUB20170826D-\$A$$-RA11.pdf";
    $list[] = "TAUC20170826D-\$A$$-RA11.pdf";
    $list[] = "TAUD20170826D-\$A$$-RA11.pdf";
    $list[] = "TBTP20170826D-\$A$$-RA11.pdf";
    $list[] = "TCBY20170826D-\$A$$-RA11.pdf";*/
    $list = $html->find('a');
    $abbr = [
        "TALB" => "ALBANY",
        "TAP" => "Asia Pacific",
    ];
    foreach ($list as &$el) {
        $el->href = strtr($el->href, $abbr);
    }
    

    Demo

    Or use array_map(), maybe you'll find it a bit cleaner:

    $list = array_map(function($el) use($abbr) {
        return strtr($el, $abbr);
    }, $list);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络