【问题标题】:Replace part of link pulled from external div替换从外部 div 中提取的部分链接
【发布时间】:2015-02-24 11:08:51
【问题描述】:

我有以下代码来提取外部 div 的内容并显示在我的网页上:

第一个将输出链接中的首字母大写的 som 样式:

<style>
h1:first-letter {text-transform: uppercase;} /* make first letter in link upper-case */
h1 {margin-bottom: -15px; font-size:1.2em;}
p {margin-bottom: 5px;}
a {text-decoration: none;}
</style>

然后是 php 代码(根据@Bang 的回答进行了最终更改):

<?php   
    //The URL for the external content we want to pull
    $url = 'https://ekstern.vuq.visma.com/ra_recruitment/';
    $content = file_get_contents($url);

    //The div that includes the content '<div id="divid">'
    $first_step = explode( '<div id="ide">' , $content );
    $second_step = explode("</div>" , $first_step[1] );

    //Do some magic with the URL
    $url2 = $second_step[0];

    //What do you want to replace?
    $patterns = array(
        '#\./opening;jsessionid=.*\?#',
        '#<a href=#',
        '#span(.*?)>#'
    );

    //...with what?
    $replaces = array(
        'https://ekstern.vuq.visma.com/ra_recruitment/opening?',
        '<a target="_blank" href=',
        'h1>'
    );

    //Print the final output
    echo preg_replace($patterns, $replaces, $url2), $second_step[1], $second_step[2], $second_step[3], $second_step[4], $second_step[5], $second_step[6], '<hr>';
?>

此代码在最后一行的$second_step[0] 中提取一个链接。此链接的格式如下:.opening\and_following_dynamic_link_text

我尝试使用str_replace更改网址以删除.opening并添加原始主机,然后添加/opening

我尝试在$first_step$second_step之后插入str_replace字符串,正如您在$content之后的代码中看到的那样。 但我就是无法让它工作。

有人可以帮我解决这个问题吗? 此外,最终输出包括一些特殊字符,如ø。我怎样才能让它显示为原始字符?

例如看HERE

编辑:感谢@kadam-sunil 和@bank,您的回答都帮助了我。我用有效的解决方案更新了代码!

我还设法删除了 URL 中一些不需要的文本,请将我的 cmets 引用到 @bang 的答案。

现在我正在尝试将 target="_blank" 插入到我提取的 URL 中。谁能指出我这样做的正确方向?

EDIT2:这是我要显示的原始数据

  <div id="ide">
        <div>
            <div class="openingTitle"><a href="./opening;jsessionid=E2A19018E967B4771224A9FA515AFBC0?0-1.ILinkListener-content-contentPanel-openings~view~container-openings~view-0-details"><span style="font-weight:bold;">fagarbeider</span></a></div>
            <div class="openingIngress"><p>Avdeling teknisk drift har ledig stilling som fagarbeider vann/avløp.<br/>Fast, 100 %, ledig snarest.</p></div>
            <div class="openingDetail"><i>Utlyst:&nbsp;<span>28.01.2015</span></i></div>
            <div class="openingDetail"><i>Søknadsfrist:&nbsp;<span style="color:red">01.03.2015</span></i></div>
            <div class="openingDetail"><i>Selskap:&nbsp;<span>Randaberg kommune</span></i></div>
            <div class="openingDetail"><i>Stillingstype:&nbsp;<span>Fast ansatt</span></i></div>
            <div class="openingDetail"><i>Lokasjon:&nbsp;<span>Avd. teknisk drift</span></i></div>
            <div class="openingDetail">

            </div>
        </div>

我还编辑了原始代码以反映我所做的最新更改。

谢谢!

【问题讨论】:

    标签: php url str-replace


    【解决方案1】:

    你应该影响str_replace

    $content = str_replace('./opening', 'https://ekstern.vuq.visma.com/ra_recruitment/opening', $content);
    

    使用 preg_replace 方法:

    $url = '<a href="./opening;jsessionid=E2A19018E967B4771224A9FA515AFBC0?0-1.ILinkListener-content-contentPanel-openings~view~container-openings~view-0-details"><span style="font-weight:bold;">fagarbeider</span></a>';
    
    // What do you want to replace ?
    $patterns = array(
        '#\./opening;jsessionid=.*\?#',
        '#<a href=#',
        '#span(.*?)>#'
    );
    
    // By What ?
    $replaces = array(
        'https://ekstern.vuq.visma.com/ra_recruitment/opening?',
        '<a target="_blank" href=',
        'h1>'
    );
    
    echo preg_replace($patterns, $replaces, $url);
    

    然后我得到&lt;a target="_blank" href="https://ekstern.vuq.visma.com/ra_recruitment/opening?0-1.ILinkListener-content-contentPanel-openings~view~container-openings~view-0-details"&gt;&lt;h1&gt;fagarbeider&lt;/h1&gt;&lt;/a&gt;

    而且我保持代码简单而愚蠢。

    【讨论】:

    • 谢谢,问题解决了!但随后又出现了一个新问题。从原始页面中提取的 url 似乎包含一个jsessionidstring,我需要删除它才能使其完美运行。在str-replace 之后拉取的 url 添加:https://ekstern.vuq.visma.com/ra_recruitment/opening;jsessionid=48311227429D90F89603CFA1D3EF3859?0-1.ILinkListener-content-contentPanel-openings~view~container-openings~view-0-details 我需要的是:https://ekstern.vuq.visma.com/ra_recruitment/opening?0-1.ILinkListener-content-contentPanel-openings~view~container-openings~view-0-details
    • 有没有有效的方法来解决这个问题?
    • 您可以编辑您的帖子并向我展示您的原始数据(来自 file_get_contents 的 $content)和您需要的原始输出吗?我认为有一种更优雅的方式可以达到想要的结果,只需使用 preg_replace。
    • 谢谢@bang,我已经编辑了原帖中的代码,并添加了raw-rata。我想我不会让它像我想要的那样工作,但如果它可以优化,那就完美了。
    • 谢谢,这也很好用,我已经用新代码编辑了原始帖子。
    【解决方案2】:
    <?php
    $url = 'https://ekstern.vuq.visma.com/ra_recruitment/';
    $content = file_get_contents($url);
    $content1=str_replace('./opening', 'https://ekstern.vuq.visma.com/ra_recruitment/opening', $content);
    $first_step = explode( '<div id="ide">' ,  $content1 );
    $second_step = explode("</div>" , $first_step[1] );
    
    echo $second_step[0], $second_step[1], $second_step[2], $second_step[3], $second_step[4], $second_step[5], $second_step[6];
    ?>  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-06
      相关资源
      最近更新 更多