2014-10-05 87 views
1
if ($link == true) { 
    // Links 
    $link_search = '/\[a\](.*?)\[\/a\]/i'; 

    if (preg_match_all($link_search, $text, $matches)) { 

     foreach ($matches[1] as $match) { 
      $match_decode = urldecode($match); 
      $match_url = $match_decode; 

      if (!preg_match("/http\:\/\//", $match_decode)) { 
       $match_url = 'http://' . $match_url; 
      } 

      $text = str_replace('[a]' . $match . '[/a]', '<a href="' . strip_tags($match_url) . '" target="_blank" rel="nofollow">' . $match_decode . '</a>', $text); 
     } 
    } 
} 

上面的代碼顯示了發佈的鏈接和點擊重定向到鏈接的時間。我想要顯示的目標鏈接。例如。計算器顯示在後,並點擊時需要用戶http://stackoverflow.com如何獲取URL的標題?

+1

這將需要檢索頁面並解析出其''標籤。如果目標網站的加載速度很慢,那麼您的網頁也會變得太大 - 如果您有很多鏈接... – <span class="text-secondary"> <small> <span>2014-10-05 13:30:59</span> </small> </span> </p> </div> </div> </div> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">那麼,我該如何做呢?請幫助我... – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/3965281/">ranger19</a></span> <span>2014-10-05 13:38:48</span> </small> </span> </p> </div> </div> </div> </div> </div> </article> </div> <div class="answer-title"> <span class="text-logo margin-top-sm">A</span> <h2 class="title h4">回答</h2> </div> <div class="item-description text-md markdown-body margin-bottom-40 voidso"> <article class="board-top-1 padding-top-10"> <div class="post-col vote-info"> <span class="count">0<i class="fa fa-thumbs-up"></i></span> <i class="fa fa-check fa-2x"></i> </div> <div class="post-offset"> <div class="answer fmt"> <p>嘗試更換:</p> <pre><code class="prettyprint-override">$text = str_replace('[a]' . $match . '[/a]', '<a href="' . strip_tags($match_url) . '" target="_blank" rel="nofollow">' . $match_decode . '</a>', $text); </code></pre> <p>有:</p> <pre><code class="prettyprint-override">$str = file_get_contents($match_url); preg_match("/\<title\>(.*)\<\/title\>/",$str,$title); $text = str_replace('[a]' . $match . '[/a]', '<a href="' . strip_tags($match_url) . '" target="_blank" rel="nofollow">' . $title[1] . '</a>', $text); </code></pre> <p>獲得的圖像:</p> <pre><code class="prettyprint-override">preg_match("/\<img\>(.*)\<\/img\>/",$str,$img); </code></pre> <p>您將得到一個在$ img中的數組,你將不得不循環它:</p> <pre><code class="prettyprint-override">for($i=0; $i<count($img); $i++){ echo $img[$i]; } </code></pre> </div> <div class="post-info"> <div class="post-meta row"> <p class="text-secondary col-lg-6"> <span class="source"> <a rel="noopener" target="_blank" href="https://stackoverflow.com/q/26203223">來源</a> </span> </p> <p class="text-secondary col-lg-6"> <span class="float-right date"> <span>2014-10-05 13:56:17</span> </p> <p class="col-12"></p> <p class="col-12"></p></div> </div> <!-- comments --> <div class="comments"> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">非常感謝...... D – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/3965281/">ranger19</a></span> <span>2014-10-05 14:33:39</span> </small> </span> </p> </div> </div> </div> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">對不起,您再次遇到問題,但您能否告訴我是否有方法提取摘錄或圖片(如果頁面有任何內容)?謝謝。 – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/3965281/">ranger19</a></span> <span>2014-10-06 06:12:40</span> </small> </span> </p> </div> </div> </div> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">使用preg_match(「/ \ <img\>(。*)\ <\/img\> /」,$ str,$ img); – <span class="text-secondary"> <small> <span>2014-10-06 06:42:29</span> </small> </span> </p> </div> </div> </div> </div> </div> </article> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1038284119" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <div class="clearfix"> </div> <div class="relative-box"> <div class="relative">相關問題</div> <ul class="relative_list"> <li> 1. <a href="http://hk.uwenku.com/question/p-rsrjjghv-eg.html" target="_blank" title="如何獲取URL「標題」標記值?"> 如何獲取URL「標題」標記值? </a> </li> <li> 2. <a href="http://hk.uwenku.com/question/p-cxblowmn-eu.html" target="_blank" title="如何獲取URL的標題"> 如何獲取URL的標題 </a> </li> <li> 3. <a href="http://hk.uwenku.com/question/p-tesodavd-wp.html" target="_blank" title="獲取不含HTML的URL標題"> 獲取不含HTML的URL標題 </a> </li> <li> 4. <a href="http://hk.uwenku.com/question/p-auqbzkvg-bnt.html" target="_blank" title="如何在phpbb中獲取url的論壇標題?"> 如何在phpbb中獲取url的論壇標題? </a> </li> <li> 5. <a href="http://hk.uwenku.com/question/p-nrryaihi-co.html" target="_blank" title="如何使用ajax獲取URL的頁面標題"> 如何使用ajax獲取URL的頁面標題 </a> </li> <li> 6. <a href="http://hk.uwenku.com/question/p-dwjhxdus-zm.html" target="_blank" title="如何攔截並獲取重定向URL的位置標題?"> 如何攔截並獲取重定向URL的位置標題? </a> </li> <li> 7. <a href="http://hk.uwenku.com/question/p-zpltmmgx-bnx.html" target="_blank" title="Excel VBA獲取網站標題從url"> Excel VBA獲取網站標題從url </a> </li> <li> 8. <a href="http://hk.uwenku.com/question/p-dxrdejfa-bhm.html" target="_blank" title="如何從url獲取html標籤?"> 如何從url獲取html標籤? </a> </li> <li> 9. <a href="http://hk.uwenku.com/question/p-temtyvgk-d.html" target="_blank" title="Angular js如何設置標題在新窗口中獲取url"> Angular js如何設置標題在新窗口中獲取url </a> </li> <li> 10. <a href="http://hk.uwenku.com/question/p-gbqmqudj-nb.html" target="_blank" title="如何從UITableView獲取URL和頁面標題?"> 如何從UITableView獲取URL和頁面標題? </a> </li> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-6208739752673518" data-ad-slot="4606349252"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <li> 11. <a href="http://hk.uwenku.com/question/p-plykxhuh-yd.html" target="_blank" title="如何獲取歌曲標題? Android的"> 如何獲取歌曲標題? Android的 </a> </li> <li> 12. <a href="http://hk.uwenku.com/question/p-xbudddqg-vb.html" target="_blank" title="如何獲取Joomla文章的標題?"> 如何獲取Joomla文章的標題? </a> </li> <li> 13. <a href="http://hk.uwenku.com/question/p-wycyogpz-bd.html" target="_blank" title="如何獲取jquery中的標題值"> 如何獲取jquery中的標題值 </a> </li> <li> 14. <a href="http://hk.uwenku.com/question/p-cqtwlmeg-bn.html" target="_blank" title="如何獲取網站的標題?"> 如何獲取網站的標題? </a> </li> <li> 15. <a href="http://hk.uwenku.com/question/p-xvfyeovz-pa.html" target="_blank" title="如何獲取SharePoint列表的標題?"> 如何獲取SharePoint列表的標題? </a> </li> <li> 16. <a href="http://hk.uwenku.com/question/p-xkczmizq-bnz.html" target="_blank" title="如何獲取ListBox控件的標題"> 如何獲取ListBox控件的標題 </a> </li> <li> 17. <a href="http://hk.uwenku.com/question/p-ybitsnlh-hs.html" target="_blank" title="如何獲取html頁面的標題"> 如何獲取html頁面的標題 </a> </li> <li> 18. <a href="http://hk.uwenku.com/question/p-paocvfeg-ym.html" target="_blank" title="如何獲取Webform的標題?"> 如何獲取Webform的標題? </a> </li> <li> 19. <a href="http://hk.uwenku.com/question/p-vaptnqyt-vd.html" target="_blank" title="獲取URL,通過Javascript獲取標題不是jQuery"> 獲取URL,通過Javascript獲取標題不是jQuery </a> </li> <li> 20. <a href="http://hk.uwenku.com/question/p-dqsqgfau-dr.html" target="_blank" title="如何獲取HTTPResponse的URL"> 如何獲取HTTPResponse的URL </a> </li> <li> 21. <a href="http://hk.uwenku.com/question/p-wwgmlynn-we.html" target="_blank" title="這個Javascript的「獲取URL和標題」有什麼問題?"> 這個Javascript的「獲取URL和標題」有什麼問題? </a> </li> <li> 22. <a href="http://hk.uwenku.com/question/p-urwdbdnv-ne.html" target="_blank" title="在PHP中使用URL獲取網站標題(並驗證URL)"> 在PHP中使用URL獲取網站標題(並驗證URL) </a> </li> <li> 23. <a href="http://hk.uwenku.com/question/p-agigfeyv-kq.html" target="_blank" title="如何獲取tableView節標題"> 如何獲取tableView節標題 </a> </li> <li> 24. <a href="http://hk.uwenku.com/question/p-gnvpbrpw-en.html" target="_blank" title="如何獲取ng-table標題值?"> 如何獲取ng-table標題值? </a> </li> <li> 25. <a href="http://hk.uwenku.com/question/p-ezqqfqam-baq.html" target="_blank" title="如何獲取標題與流?"> 如何獲取標題與流? </a> </li> <li> 26. <a href="http://hk.uwenku.com/question/p-rzuwiomt-hn.html" target="_blank" title="如何從xhr獲取標題?"> 如何從xhr獲取標題? </a> </li> <li> 27. <a href="http://hk.uwenku.com/question/p-tfnixwdm-ra.html" target="_blank" title="如何獲取SPListItemVersion標題和值"> 如何獲取SPListItemVersion標題和值 </a> </li> <li> 28. <a href="http://hk.uwenku.com/question/p-vduoyrrb-bmk.html" target="_blank" title="如何獲取所有鏈接標題?"> 如何獲取所有鏈接標題? </a> </li> <li> 29. <a href="http://hk.uwenku.com/question/p-vbvujcko-bn.html" target="_blank" title="如何按標題獲取組ID?"> 如何按標題獲取組ID? </a> </li> <li> 30. <a href="http://hk.uwenku.com/question/p-zqswwqzl-nc.html" target="_blank" title="如何獲取datagridvier列標題名稱"> 如何獲取datagridvier列標題名稱 </a> </li> </ul> </div> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1575177025"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="padding-top-10"></div> </div> </div> <script type="text/javascript" src="http://img.uwenku.com/uwenku/script/side.js?t=1644592048261"></script> <script type="text/javascript" src="http://img.uwenku.com/uwenku/plugin/highlight/highlight.pack.js"></script> <link href="http://img.uwenku.com/uwenku/plugin/highlight/styles/docco.css" media="screen" rel="stylesheet" type="text/css" /> <script type="text/javascript"> $('pre').each(function(i, e) { hljs.highlightBlock(e, "<span class='indent'> </span>", false) }); </script> <div class="col-lg-3 col-md-4 col-sm-5"> <div id="rightTop"> <div class="row"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="5415218910" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="row sidebar panel panel-default"> <div class="panel-heading font-bold"> 最新問題 </div> <div class="m-b-sm m-t-sm clearfix"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://hk.uwenku.com/question/p-dfmtksul-vx.html" target="_blank" title="jQuery的動態CSS屬性(在滾動)"> jQuery的動態CSS屬性(在滾動) </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-vuvasezd-wd.html" target="_blank" title="Eclipse手動/脫機JBoss Tools Luna安裝:缺少需求abc需要'bundle xyz',但找不到"> Eclipse手動/脫機JBoss Tools Luna安裝:缺少需求abc需要'bundle xyz',但找不到 </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-cgymfszy-uo.html" target="_blank" title="Laravel登記錯誤,數據庫連接,但收到奇怪的錯誤"> Laravel登記錯誤,數據庫連接,但收到奇怪的錯誤 </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-qybltylb-vh.html" target="_blank" title="highmaps在遷移到.NET Core後停止更新"> highmaps在遷移到.NET Core後停止更新 </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-bkksxbpc-vq.html" target="_blank" title="攔截winsock的recvfrom函數提供了無效地址錯誤"> 攔截winsock的recvfrom函數提供了無效地址錯誤 </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-ayftxvnb-va.html" target="_blank" title="Python對象混入注射"> Python對象混入注射 </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-xwmcdgyo-sg.html" target="_blank" title="批處理文件無法正常工作,除非我正在觀看"> 批處理文件無法正常工作,除非我正在觀看 </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-xggrozic-tb.html" target="_blank" title="司 - SQL"> 司 - SQL </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-zurkldvr-tn.html" target="_blank" title="在C++ Builder中的服務應用程序6"> 在C++ Builder中的服務應用程序6 </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-xvogfpaq-py.html" target="_blank" title="Spring REST:適用於嵌套XML請求正文的構造函數嗎?"> Spring REST:適用於嵌套XML請求正文的構造函數嗎? </a> </li> </ul> </div> </div> </div> <p class="article-nav-bar"></p> <div class="row sidebar article-nav"> <div class="row box_white visible-sm visible-md visible-lg margin-zero"> <div class="top"> <h3 class="title"><i class="glyphicon glyphicon-th-list"></i> 相關問題</h3> </div> <div class="article-relative-content"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://hk.uwenku.com/question/p-rsrjjghv-eg.html" target="_blank" title="如何獲取URL「標題」標記值?"> 如何獲取URL「標題」標記值? </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-cxblowmn-eu.html" target="_blank" title="如何獲取URL的標題"> 如何獲取URL的標題 </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-tesodavd-wp.html" target="_blank" title="獲取不含HTML的URL標題"> 獲取不含HTML的URL標題 </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-auqbzkvg-bnt.html" target="_blank" title="如何在phpbb中獲取url的論壇標題?"> 如何在phpbb中獲取url的論壇標題? </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-nrryaihi-co.html" target="_blank" title="如何使用ajax獲取URL的頁面標題"> 如何使用ajax獲取URL的頁面標題 </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-dwjhxdus-zm.html" target="_blank" title="如何攔截並獲取重定向URL的位置標題?"> 如何攔截並獲取重定向URL的位置標題? </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-zpltmmgx-bnx.html" target="_blank" title="Excel VBA獲取網站標題從url"> Excel VBA獲取網站標題從url </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-dxrdejfa-bhm.html" target="_blank" title="如何從url獲取html標籤?"> 如何從url獲取html標籤? </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-temtyvgk-d.html" target="_blank" title="Angular js如何設置標題在新窗口中獲取url"> Angular js如何設置標題在新窗口中獲取url </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-gbqmqudj-nb.html" target="_blank" title="如何從UITableView獲取URL和頁面標題?"> 如何從UITableView獲取URL和頁面標題? </a> </li> </ul> </div> </div> </div> </div> </div> </div> </div><!-- wrap end--> <!-- footer --> <footer id="footer"> <div class="bg-simple lt"> <div class="container"> <div class="row padder-v m-t"> <div class="col-xs-8"> <ul class="list-inline"> <li><a href="http://hk.uwenku.com/contact">聯系我們</a></li> <li>© 2020 HK.UWENKU.COM</li> <li><a target="_blank" href="https://beian.miit.gov.cn/">沪ICP备13005482号-4</a></li> <li><script type="text/javascript" src="https://v1.cnzz.com/z_stat.php?id=1280101193&web_id=1280101193"></script></li> <li><a href="http://www.uwenku.com/" target="_blank" title="优文库">简体中文</a></li> <li><a href="http://hk.uwenku.com/" target="_blank" title="優文庫">繁體中文</a></li> <li><a href="http://ru.uwenku.com/" target="_blank" title="поле вопросов и ответов">Русский</a></li> <li><a href="http://de.uwenku.com/" target="_blank" title="Frage - und - antwort - Park">Deutsch</a></li> <li><a href="http://es.uwenku.com/" target="_blank" title="Preguntas y respuestas">Español</a></li> <li><a href="http://hi.uwenku.com/" target="_blank" title="कार्यक्रम प्रश्न और उत्तर पार्क">हिन्दी</a></li> <li><a href="http://it.uwenku.com/" target="_blank" title="IL Programma di chiedere Park">Italiano</a></li> <li><a href="http://ja.uwenku.com/" target="_blank" title="プログラム問答園区">日本語</a></li> <li><a href="http://ko.uwenku.com/" target="_blank" title="프로그램 문답 단지">한국어</a></li> <li><a href="http://pl.uwenku.com/" target="_blank" title="program o park">Polski</a></li> <li><a href="http://tr.uwenku.com/" target="_blank" title="Program soru ve cevap parkı">Türkçe</a></li> <li><a href="http://vi.uwenku.com/" target="_blank" title="Đáp ứng viên">Tiếng Việt</a></li> <li><a href="http://fr.uwenku.com/" target="_blank" title="Programme interrogation Park">Française</a></li> </ul> </div> </div> </div> </div> </div> </footer> <!-- / footer --> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?f78a970f17b19a79fc477a3378096f29"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>