我有以下代码来提取外部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
。
我尝试在str_replace
和$first_step
之后插入$second_step
字符串,正如您在$content
之后的代码中看到的那样。
但我无法让它发挥作用。
有人可以帮我解决这个问题吗?
此外,最终输出包括一些显示为ø
的特殊字符。如何将其显示为原始角色?
以示例查看HERE。
编辑:感谢@ kadam-sunil和@bank,您的回答都帮助了我。我使用工作解决方案更新了代码!
我还设法删除了网址中的一些不需要的文字,将我的评论引用到@ 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>
我还编辑了原始代码,以反映我所做的最新更改。
谢谢!
答案 0 :(得分: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);
然后我得到<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>
我保持代码简单而愚蠢。
答案 1 :(得分:1)
<?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];
?>