有的 网站 源码中加入了这几行代码: 1 link rel ="shortcut icon" href ="/favicon.ico" type ="image/x-icon" / 或者是 1 link rel =" icon" href ="/favicon.ico" type ="image/x-icon" / 这样可以指定 图标 位置存放。 但现在的浏览器大多支持自动 获取
有的网站源码中加入了这几行代码:
<span>1</span> <span><</span><span>link </span><span>rel</span><span>="shortcut icon"</span><span> href</span><span>="/favicon.ico"</span><span> type</span><span>="image/x-icon"</span> <span>/></span>
或者是
<span>1</span> <span><</span><span>link </span><span>rel</span><span>=" icon"</span><span> href</span><span>="/favicon.ico"</span><span> type</span><span>="image/x-icon"</span> <span>/></span>
这样可以指定图标位置存放。
但现在的浏览器大多支持自动获取网站根目录下的.ico文件,所以源码中看不到那几行代码。
可行的方法:
<span> 1</span> <span>//</span><span>得到每一个url,<strong>获取</strong>其中的icon</span> <span> 2</span> <span>$url</span> = <span>$matches</span>[1][<span>$i</span><span>]; </span><span> 3</span> <span>$contents</span> = @<span>file_get_contents</span>(<span>$url</span><span>); </span><span> 4</span> <span>preg_match</span>('/<link rel=".*?icon".*?href="(.*?)".*?>/', <span>$contents</span>,<span>$icon</span><span>); </span><span> 5</span> <span>if</span>(!<span>empty</span>(<span>$icon</span><span>)){ </span><span> 6</span> <span>//</span><span> print_r($icon[1]);</span> <span> 7</span> <span>echo</span> "<br/>"<span>; </span><span> 8</span> <span>$array</span> = @<span>get_headers</span>(<span>$icon</span>[1],1<span>); </span><span> 9</span> <span>if</span>(<span>preg_match</span>('/200/',<span>$array</span>[0<span>])){ </span><span>10</span> <span>//</span><span> echo "<pre/>"; </span> <span>11</span> <span>print_r</span>(<span>$icon</span>[1<span>]); </span><span>12</span> <span>echo</span> "<img src='".<span>$icon</span>[1]."' >; <span>13</span> <span>//</span><span> echo "string"; </span><span>14</span> <span> // print_r($array); </span> <span>15</span> }<span>else</span><span>{ </span><span>16</span> <span>//</span><span>去掉多余的斜杠</span> <span>17</span> <span>$url</span> = <span>substr_replace</span>(<span>$url</span>,"",-1<span>); </span><span>18</span> <span>echo</span> "无效url资源!".<span>$url</span>.<span>$icon</span>[1<span>]; </span><span>19</span> <span>echo</span> "<img src='".<span>$url</span>.<span>$icon</span>[1]."' >; <span>20</span> <span> } </span><span>21</span> <span>22</span> }<span>else</span><span>{ </span><span>23</span> <span>//</span><span>这里笔者试了很多种方法,后来发现通过url可以入手,从url截取网址的根目录,由于习惯性的把<strong>文件</strong>名名为favicon.ico,所以可以直接访问类似这样的url得到<strong>图标</strong><strong>文件</strong>,http://wenwen.sogou.com/favicon.ico</span> <span>24</span> <span>$url</span>=<span>substr</span>(<span>$url</span>,7);<span>//</span><span>去除前面</span> <span>25</span> <span>$position</span> = <span>strpos</span>(<span>$url</span>, '/'<span>); </span><span>26</span> <span>$url</span>=<span>substr</span>(<span>$url</span>,0,<span>$position</span><span>); </span><span>27</span> <span>echo</span> "<img src='http://".<span>$url</span>."/favicon.ico' >; <span>28</span> <span>//</span><span> echo $url."/favicon.ico<br/>"; </span><span>29</span> <span> //判断<strong>网站</strong>的根目录是否存在.icon<strong>文件</strong> </span><span>30</span> <span> // $url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]; </span><span>31</span> <span> // echo dirname($url); </span><span>32</span> <span> // if(file_exists("favicon.ico")) </span><span>33</span> <span> // echo "<p>存在</p>";</span> <span>34</span> }
思路:
先判断源码中是否有那几行添加iocn的代码,如果有就直接抓取代码,再判断是否为有效的url,然后再判断显示,没有则获取根目录后显示。