替换从外部 div 拉取的 link 的一部分

Replace part of link pulled from external div

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

第一个 som 样式使输出中的第一个字母大写-link:

<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] 中拉出一个 link。 link 的格式如下:.opening\and_following_dynamic_link_text.

我尝试使用 str_replace 来改变 url 以删除 .opening 并添加原始主机,然后添加 /opening

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

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

查看示例 HERE

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

我还设法删除了 URL 中一些不需要的文本,请参考@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>

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

谢谢!

你应该影响 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);

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

并且我保持代码简单和愚蠢。

<?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];
?>