2012-11-17 146 views
1

感謝您花一秒鐘來看看這個。我正在使用PHP腳本從URL獲取頁面的源代碼,然後我試圖解析它並顯示特定的部分文本。這個問題似乎是,當我得到的源鏈接(附:PHP解析HTML中的字符串

$data = file_get_contents($link); 

)變量$的數據存儲爲HTML而不是隻是一個字符串。我對PHP很新,所以我不確定是否如此,但我知道如果我試圖以任何方式顯示$ data,它不會顯示爲純文本,而是顯示爲HTML格式的HTML。

按說這不會是一個問題,但我試圖得到一個HTML標籤內的東西的價值,就像這樣:

$search = strpos($data, $searchterm); 

因爲它要麼存儲爲HTML而不是純文本或者以這種方式處理,strpos()將只搜索如果我加載頁面時將顯示的文本。

更具體地講,在我的文件(關於我的帳戶的YouTube數據),也只是看看什麼會顯示,如果它是被加載HTML,這是純粹的無稽之談。

這裏是我希望它通過搜索(我已經取代我的帳戶名稱與「我的帳戶」隱私)來源:

<entry gd:etag="W/"A0MFR347eCp7I2A9WhNQEU4."" xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:yt="http://gdata.youtube.com/schemas/2007"> 
<id>tag:youtube.com,2008:user:A1RDBCYeYWY9dydB9MmPlg</id> 
<published>2007-01-23T15:39:30.000Z</published> 
<updated>2012-11-17T08:03:36.000Z</updated> 
<category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#userProfile"/> 
<title>MyAccount</title> 
<summary/> 
<link rel="alternate" type="text/html" href="http://www.youtube.com/channel/UCA1RDBCYeYWY9dydB9MmPlg"/> 
<link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/A1RDBCYeYWY9dydB9MmPlg?v=2"/> 
<author> 
<name>MyAccount</name> 
<uri>http://gdata.youtube.com/feeds/api/users/MyAccount</uri> 
<yt:userId>A1RDBCYeYWY9dydB9MmPlg</yt:userId> 
</author> 
<yt:channelId>UCA1RDBCYeYWY9dydB9MmPlg</yt:channelId> 
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.liveevent" href="http://gdata.youtube.com/feeds/api/users/MyAccount/live/events?v=2" countHint="0"/> 
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.favorites" href="http://gdata.youtube.com/feeds/api/users/MyAccount/favorites?v=2" countHint="0"/> 
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.contacts" href="http://gdata.youtube.com/feeds/api/users/MyAccount/contacts?v=2" countHint="71"/> 
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.inbox" href="http://gdata.youtube.com/feeds/api/users/MyAccount/inbox?v=2"/> 
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.playlists" href="http://gdata.youtube.com/feeds/api/users/MyAccount/playlists?v=2"/> 
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.subscriptions" href="http://gdata.youtube.com/feeds/api/users/MyAccount/subscriptions?v=2" countHint="54"/> 
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.uploads" href="http://gdata.youtube.com/feeds/api/users/MyAccount/uploads?v=2" countHint="41"/> 
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.newsubscriptionvideos" href="http://gdata.youtube.com/feeds/api/users/MyAccount/newsubscriptionvideos?v=2"/> 
<yt:location>US</yt:location> 
<yt:maxUploadDuration seconds="43200"/> 
<yt:statistics lastWebAccess="2012-07-08T15:58:07.000Z" subscriberCount="126" videoWatchCount="0" viewCount="3385" totalUploadViews="50179"/> 
<media:thumbnail url="http://i2.ytimg.com/i/A1RDBCYeYWY9dydB9MmPlg/1.jpg?v=934f35"/> 
<yt:userId>A1RDBCYeYWY9dydB9MmPlg</yt:userId> 
<yt:username display="MyAccount">MyAccount</yt:username> 
</entry> 

,這裏是什麼搜索/有權訪問:

tag:youtube.com,2008:user:A1RDBCYeYWY9dydB9MmPlg2007-01-23T15:39:30.000Z2012-11-17T08:03:36.000Z 
MyAccounthttp://gdata.youtube.com/feeds/api/users/MyAccountA1RDBCYeYWY9dydB9MmPlgUCA1RDBCYeYWY9dydB9MmPlgUSA1RDBCYeYWY9dydB9MmPlgMyAccount 

任何及所有幫助是極大的讚賞!

+1

一字,三個字母:['DOM'](http://php.net/manual/en /book.dom.php) – rdlowrey

+0

我們需要重複多少次? **不要將HTML/XML解析爲文本。** – Christian

+0

嘗試:$ search = strpos($ data,'');或嘗試preg_match與模式 – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/1140407/">Shiv</a></span> <span></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">$data = file_get_contents($link); $searchterm = ''; //as necessary $data = strtr($data,Array("<"=>"&lt;","&"=>"&amp;")); $searchterm = strtr($searchterm,Array("<"=>"&lt;","&"=>"&amp;")); $search = strpos($data, $searchterm); </code></pre> <p>中間線使得HTML可讀爲PHP處理</p> </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/13434437">來源</a> </span> </p> <p class="text-secondary col-lg-6"> <span class="float-right date"> <span>2012-11-17 20:19:59</span> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/903454/">5hahiL</a></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">這個偉大的作品,使我顯示當我嘗試 echo($ data); 但它仍然不會讓strpos();搜索整個事情,任何想法? – <span class="text-secondary"> <small> <span></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">您是否嘗試使用處理$ searchterm的行進行編輯? 這是必要的,因爲<將與<比較並且會失敗。 – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/903454/">5hahiL</a></span> <span></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">剛剛嘗試編輯,它完美的作品!非常感謝幫助新手! :D – <span class="text-secondary"> <small> <span></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-uvioncyq-bhy.html" target="_blank" title="解析HTML字符串的PHP方法"> 解析HTML字符串的PHP方法 </a> </li> <li> 2. <a href="http://hk.uwenku.com/question/p-plorzzla-mn.html" target="_blank" title="解析HTML字符串"> 解析HTML字符串 </a> </li> <li> 3. <a href="http://hk.uwenku.com/question/p-alyegcxr-qk.html" target="_blank" title="Linq解析html字符串"> Linq解析html字符串 </a> </li> <li> 4. <a href="http://hk.uwenku.com/question/p-diqwirvk-kt.html" target="_blank" title="解析java servlet中的html字符串"> 解析java servlet中的html字符串 </a> </li> <li> 5. <a href="http://hk.uwenku.com/question/p-muepdthi-zq.html" target="_blank" title="解析HTML字符串中的ios"> 解析HTML字符串中的ios </a> </li> <li> 6. <a href="http://hk.uwenku.com/question/p-kvqwskev-bem.html" target="_blank" title="解析JSON字符串IOS - HTML字符"> 解析JSON字符串IOS - HTML字符 </a> </li> <li> 7. <a href="http://hk.uwenku.com/question/p-zpnwtlns-us.html" target="_blank" title="PHP JSON字符串解析"> PHP JSON字符串解析 </a> </li> <li> 8. <a href="http://hk.uwenku.com/question/p-bfkfmjrp-bcs.html" target="_blank" title="PHP解析JSON字符串"> PHP解析JSON字符串 </a> </li> <li> 9. <a href="http://hk.uwenku.com/question/p-sujrwkai-bae.html" target="_blank" title="PHP字符串解析"> PHP字符串解析 </a> </li> <li> 10. <a href="http://hk.uwenku.com/question/p-xewquofn-bbh.html" target="_blank" title="PHP - 解析字符串"> PHP - 解析字符串 </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-kvionimk-bke.html" target="_blank" title="PHP解析XML字符串"> PHP解析XML字符串 </a> </li> <li> 12. <a href="http://hk.uwenku.com/question/p-dstsuevm-xs.html" target="_blank" title="在PHP中的字符串解析"> 在PHP中的字符串解析 </a> </li> <li> 13. <a href="http://hk.uwenku.com/question/p-knlyvgso-ca.html" target="_blank" title="解析js html庫中的字符串(語言字符串)"> 解析js html庫中的字符串(語言字符串) </a> </li> <li> 14. <a href="http://hk.uwenku.com/question/p-nxjjlrrl-vt.html" target="_blank" title="Android,解析html,字符串中的字符串問題"> Android,解析html,字符串中的字符串問題 </a> </li> <li> 15. <a href="http://hk.uwenku.com/question/p-ywgqmzim-d.html" target="_blank" title="在Swift中解析HTML字符串"> 在Swift中解析HTML字符串 </a> </li> <li> 16. <a href="http://hk.uwenku.com/question/p-mpimnwor-bcs.html" target="_blank" title="解析字符串中的字符串php"> 解析字符串中的字符串php </a> </li> <li> 17. <a href="http://hk.uwenku.com/question/p-rmynbmlx-zr.html" target="_blank" title="UIWebView加載解析的html字符串"> UIWebView加載解析的html字符串 </a> </li> <li> 18. <a href="http://hk.uwenku.com/question/p-sxwfdbel-bdo.html" target="_blank" title="在PHP中解析JSON字符串「gdata.io.handleScriptLoaded」"> 在PHP中解析JSON字符串「gdata.io.handleScriptLoaded」 </a> </li> <li> 19. <a href="http://hk.uwenku.com/question/p-xrwmzwxp-gm.html" target="_blank" title="在PHP中解析字符串"> 在PHP中解析字符串 </a> </li> <li> 20. <a href="http://hk.uwenku.com/question/p-payaqwgg-qn.html" target="_blank" title="PHP將字符串轉換爲html並解析html文件"> PHP將字符串轉換爲html並解析html文件 </a> </li> <li> 21. <a href="http://hk.uwenku.com/question/p-ygyxraci-kt.html" target="_blank" title="解析解析字符串"> 解析解析字符串 </a> </li> <li> 22. <a href="http://hk.uwenku.com/question/p-vveolchu-bmr.html" target="_blank" title="PHP + Smarty:將PHP + HTML解析爲字符串?"> PHP + Smarty:將PHP + HTML解析爲字符串? </a> </li> <li> 23. <a href="http://hk.uwenku.com/question/p-wexvndkg-zh.html" target="_blank" title="PHP解析器 - 在HTML中查找字符串"> PHP解析器 - 在HTML中查找字符串 </a> </li> <li> 24. <a href="http://hk.uwenku.com/question/p-auufcslw-en.html" target="_blank" title="PHP複雜的字符串解析"> PHP複雜的字符串解析 </a> </li> <li> 25. <a href="http://hk.uwenku.com/question/p-walhigfc-og.html" target="_blank" title="PHP自動解析我的字符串?"> PHP自動解析我的字符串? </a> </li> <li> 26. <a href="http://hk.uwenku.com/question/p-whuutnjc-zx.html" target="_blank" title="PHP解析iCal的提示字符串"> PHP解析iCal的提示字符串 </a> </li> <li> 27. <a href="http://hk.uwenku.com/question/p-vxhdfikn-bey.html" target="_blank" title="簡單的PHP字符串解析"> 簡單的PHP字符串解析 </a> </li> <li> 28. <a href="http://hk.uwenku.com/question/p-btohnhcw-bkq.html" target="_blank" title="解析字符串中的字符"> 解析字符串中的字符 </a> </li> <li> 29. <a href="http://hk.uwenku.com/question/p-tvuppewl-nb.html" target="_blank" title="解析大字符串(HTML代碼)"> 解析大字符串(HTML代碼) </a> </li> <li> 30. <a href="http://hk.uwenku.com/question/p-yzvitxqj-bmq.html" target="_blank" title="Matlab文本字符串/ html解析"> Matlab文本字符串/ html解析 </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-uvioncyq-bhy.html" target="_blank" title="解析HTML字符串的PHP方法"> 解析HTML字符串的PHP方法 </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-plorzzla-mn.html" target="_blank" title="解析HTML字符串"> 解析HTML字符串 </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-alyegcxr-qk.html" target="_blank" title="Linq解析html字符串"> Linq解析html字符串 </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-diqwirvk-kt.html" target="_blank" title="解析java servlet中的html字符串"> 解析java servlet中的html字符串 </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-muepdthi-zq.html" target="_blank" title="解析HTML字符串中的ios"> 解析HTML字符串中的ios </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-kvqwskev-bem.html" target="_blank" title="解析JSON字符串IOS - HTML字符"> 解析JSON字符串IOS - HTML字符 </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-zpnwtlns-us.html" target="_blank" title="PHP JSON字符串解析"> PHP JSON字符串解析 </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-bfkfmjrp-bcs.html" target="_blank" title="PHP解析JSON字符串"> PHP解析JSON字符串 </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-sujrwkai-bae.html" target="_blank" title="PHP字符串解析"> PHP字符串解析 </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-xewquofn-bbh.html" target="_blank" title="PHP - 解析字符串"> PHP - 解析字符串 </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>