【发布时间】: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: <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>
我还编辑了原始代码以反映我所做的最新更改。
谢谢!
【问题讨论】:
标签: php url str-replace