拉着我有下面的代碼提取一個外部的div並顯示在我的網頁上的內容:替換鏈接的一部分來自外部的div
一次高級官員會議的風格,使第一個字母大寫輸出 - 鏈接:
<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代碼:
<?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>';
?>
此代碼拉在01的鏈接最後一行是。此鏈接的格式如下:.opening\and_following_dynamic_link_text
。
我嘗試使用str_replace
來更改url以刪除.opening
並添加原始主機,然後添加/opening
。
我試圖在$first_step
和$second_step
之後插入str_replace
字符串,並且您可以在$content
之後的代碼中看到。 但我只是無法得到這個工作。
有人可以幫我解決這個問題嗎? 此外,最終輸出包含一些特殊字符,顯示如ø
。我怎樣才能讓它顯示爲原始字符?
例如查看HERE。
編輯:謝謝@ kadam-sunil和@bank,你的答案幫助我在這裏。我用工作解決方案更新了代碼!
我也設法刪除網址中的一些不需要的文本,請將我的評論引用到@ bang的回答中。
現在我試圖在我提取的URL中插入一個target =「_ blank」。任何人都可以指出我正確的方向來做到這一點嗎?
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: <span>28.01.2015</span></i></div>
<div class="openingDetail"><i>Søknadsfrist: <span style="color:red">01.03.2015</span></i></div>
<div class="openingDetail"><i>Selskap: <span>Randaberg kommune</span></i></div>
<div class="openingDetail"><i>Stillingstype: <span>Fast ansatt</span></i></div>
<div class="openingDetail"><i>Lokasjon: <span>Avd. teknisk drift</span></i></div>
<div class="openingDetail">
</div>
</div>
我還編輯了原始代碼,以反映我所做的最新變化。
謝謝!
謝謝,解決了這個問題!但隨後出現了一個新問題。 似乎從原始頁面拉出的網址包含一個'jsessionid'字符串,我需要刪除才能完美工作。 添加了'str-replace'後的網址: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-開口〜查看0-details' – Thorbj 2015-02-24 11:23:02
有沒有一種有效的方法來解決這個問題? – Thorbj 2015-02-24 11:25:24
你可以編輯你的文章並顯示你的原始數據($ file_get_contents中的內容)和你需要的原始輸出嗎?我認爲有一種更優雅的方式來達到想要的結果,只需使用preg_replace即可。 – Bang 2015-02-24 13:19:34