2012-09-08 102 views
3

我將如何在php中編寫一個php preg_match()來挑選出250值。我有一大串html代碼,我想從中選出250個,我似乎無法獲得正確的表達式。從一些html代碼preg_match

這是我想匹配的HTML模式 - 注意,我要提取的整數,其中250爲:

<span class="price-ld">H$250</span>

我一直在嘗試了幾個小時要做到這一點,我不能讓它工作大聲笑

回答

1

這是你要找的正則表達式:

(?<=<span class="price-ld">H\$)\d+(?=</span>) 

你可以看到的結果here

而這裏的解釋:

Options: case insensitive;^and $ match at line breaks 

Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) «(?<=<span class="price-ld">H\$)» 
    Match the characters 「<span class="price-ld">H」 literally «<span class="price-ld">H» 
    Match the character 「$」 literally «\$» 
Match a single digit 0..9 «\d+» 
    Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=</span>)» 
    Match the characters 「</span>」 literally «span>» 
3
preg_match('/<span class="price-ld">H$(\d+)<\/span>/i', $your_html, $matches); 
print "Its ".$matches[1]." USD"; 

正則表達式實際上取決於您的代碼。你到底在哪裏搜索?

+0

我硬是要拉任何數量的H $它是由特定的模式與'類=「價格-LD」' –

+0

它獲得包圍後到來匹配:'//獲得產品的標題 preg_match('/ (。*)<\/title>/i',$ candidate [0],$ title);$ p_title = $ title [1];' – <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">但是這個並不是:'//得到產品的價格 preg_match('/ <span class =「price-ld」> H $(\ d +)<\/span>/i ',$ candidate [0],$ price); $ p_price = $ price [1]; echo $ p_price;' – <span class="text-secondary"> <small> <span></span> </small> </span> </p> </div> </div> </div> </div> </div> </article> </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-wcauhttg-bu.html" target="_blank" title="PHP從html解析一些代碼"> PHP從html解析一些代碼 </a> </li> <li> 2. <a href="http://hk.uwenku.com/question/p-dfazhfxh-tr.html" target="_blank" title="從源代碼的preg_match"> 從源代碼的preg_match </a> </li> <li> 3. <a href="http://hk.uwenku.com/question/p-dbrqcpgv-qk.html" target="_blank" title="提取與一些的preg_match HTML"> 提取與一些的preg_match HTML </a> </li> <li> 4. <a href="http://hk.uwenku.com/question/p-keyrolug-yd.html" target="_blank" title="用PHP收集一些部分HTML(preg_match)"> 用PHP收集一些部分HTML(preg_match) </a> </li> <li> 5. <a href="http://hk.uwenku.com/question/p-ndmstuep-hc.html" target="_blank" title="Javascript中的preg_match - 從代碼中獲取src(非標準html代碼)"> Javascript中的preg_match - 從代碼中獲取src(非標準html代碼) </a> </li> <li> 6. <a href="http://hk.uwenku.com/question/p-wizczusf-hr.html" target="_blank" title="從HTML用的preg_match"> 從HTML用的preg_match </a> </li> <li> 7. <a href="http://hk.uwenku.com/question/p-akdjxofa-bdk.html" target="_blank" title="php代碼跳過一些html內容"> php代碼跳過一些html內容 </a> </li> <li> 8. <a href="http://hk.uwenku.com/question/p-cxdkkyjp-yd.html" target="_blank" title="使用jquery刪除一些html代碼"> 使用jquery刪除一些html代碼 </a> </li> <li> 9. <a href="http://hk.uwenku.com/question/p-vopmvqes-hr.html" target="_blank" title="從HTML代碼"> 從HTML代碼 </a> </li> <li> 10. <a href="http://hk.uwenku.com/question/p-fdgxvhqo-uz.html" target="_blank" title="從HTML代碼"> 從HTML代碼 </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-sxybkgcc-ks.html" target="_blank" title="我有一些HTML和PHP代碼中的一些錯誤"> 我有一些HTML和PHP代碼中的一些錯誤 </a> </li> <li> 12. <a href="http://hk.uwenku.com/question/p-yehcbvpc-bks.html" target="_blank" title="從互聯網獲取html源代碼時的一些問題"> 從互聯網獲取html源代碼時的一些問題 </a> </li> <li> 13. <a href="http://hk.uwenku.com/question/p-scgprugy-bbg.html" target="_blank" title="HTML代碼(一)"> HTML代碼(一) </a> </li> <li> 14. <a href="http://hk.uwenku.com/question/p-ahcglmzb-gt.html" target="_blank" title="的preg_match一些字符"> 的preg_match一些字符 </a> </li> <li> 15. <a href="http://hk.uwenku.com/question/p-ecbntszi-bgp.html" target="_blank" title="的preg_match排除一些"> 的preg_match排除一些 </a> </li> <li> 16. <a href="http://hk.uwenku.com/question/p-oteuibbj-xn.html" target="_blank" title="preg_match只有一些數字"> preg_match只有一些數字 </a> </li> <li> 17. <a href="http://hk.uwenku.com/question/p-ksiwppid-kz.html" target="_blank" title="如何preg_match這些HTML標籤?"> 如何preg_match這些HTML標籤? </a> </li> <li> 18. <a href="http://hk.uwenku.com/question/p-mlvitvvc-yx.html" target="_blank" title="如何用Javascript替換另一個HTML代碼中的某些HTML代碼"> 如何用Javascript替換另一個HTML代碼中的某些HTML代碼 </a> </li> <li> 19. <a href="http://hk.uwenku.com/question/p-dktelchc-bdg.html" target="_blank" title="一些JavaScript代碼"> 一些JavaScript代碼 </a> </li> <li> 20. <a href="http://hk.uwenku.com/question/p-zzepfpka-er.html" target="_blank" title="XPATH:從HTML代碼"> XPATH:從HTML代碼 </a> </li> <li> 21. <a href="http://hk.uwenku.com/question/p-hwupuuup-bbk.html" target="_blank" title="轉換從C++一些代碼至C"> 轉換從C++一些代碼至C </a> </li> <li> 22. <a href="http://hk.uwenku.com/question/p-nyxfnyxv-oy.html" target="_blank" title="從jQuery重寫一些代碼到Angular"> 從jQuery重寫一些代碼到Angular </a> </li> <li> 23. <a href="http://hk.uwenku.com/question/p-ubamnned-kp.html" target="_blank" title="一些html標籤的參數messup html/php代碼"> 一些html標籤的參數messup html/php代碼 </a> </li> <li> 24. <a href="http://hk.uwenku.com/question/p-tucanpbn-g.html" target="_blank" title="Jsoup - 使用這些代碼行從Web改善從HTML頁面"> Jsoup - 使用這些代碼行從Web改善從HTML頁面 </a> </li> <li> 25. <a href="http://hk.uwenku.com/question/p-panpagzs-wv.html" target="_blank" title="從asp.net代碼獲取一些網站的源代碼"> 從asp.net代碼獲取一些網站的源代碼 </a> </li> <li> 26. <a href="http://hk.uwenku.com/question/p-yxvywsgf-bkz.html" target="_blank" title="的preg_match HTML代碼<request></request>"> 的preg_match HTML代碼<request></request> </a> </li> <li> 27. <a href="http://hk.uwenku.com/question/p-hbxovkdw-eo.html" target="_blank" title="HTML CSS代碼從代碼筆給"> HTML CSS代碼從代碼筆給 </a> </li> <li> 28. <a href="http://hk.uwenku.com/question/p-mgxdqwga-hd.html" target="_blank" title="Python代碼從html代碼中消失"> Python代碼從html代碼中消失 </a> </li> <li> 29. <a href="http://hk.uwenku.com/question/p-khbkmkbf-bga.html" target="_blank" title="代碼中的Html代碼一"> 代碼中的Html代碼一 </a> </li> <li> 30. <a href="http://hk.uwenku.com/question/p-uvnasdtg-ep.html" target="_blank" title="下載HTML代碼的某些部分"> 下載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-vewurpqk-nz.html" target="_blank" title="TensorFlow - 恢復(如果存在)"> TensorFlow - 恢復(如果存在) </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-qdizxzsl-ok.html" target="_blank" title="阿帕奇氣流1.9從GitHub"> 阿帕奇氣流1.9從GitHub </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-xlwltvrj-ne.html" target="_blank" title="在add_custom_command中連接多個文件"> 在add_custom_command中連接多個文件 </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-ttdaxkmp-nq.html" target="_blank" title="Laravel 5/Form安全(需要澄清)"> Laravel 5/Form安全(需要澄清) </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-ghukbgjo-mm.html" target="_blank" title="如何用虛假來表示可空值?"> 如何用虛假來表示可空值? </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-fxocyrsj-mv.html" target="_blank" title="Excel的數字格式損壞"> Excel的數字格式損壞 </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-xphnnpes-ma.html" target="_blank" title="Activemq - 通過不穩定連接發送大文件的最佳協議"> Activemq - 通過不穩定連接發送大文件的最佳協議 </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-oxpejcyj-hn.html" target="_blank" title="「JBoss Tools Java Standard Tools AngularJS」將被忽略,因爲它已被安裝"> 「JBoss Tools Java Standard Tools AngularJS」將被忽略,因爲它已被安裝 </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-vhysnhuo-ke.html" target="_blank" title="命令超時| Discord.js"> 命令超時| Discord.js </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-oqwfsoag-cy.html" target="_blank" title="如何獲取此數據?"> 如何獲取此數據? </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-wcauhttg-bu.html" target="_blank" title="PHP從html解析一些代碼"> PHP從html解析一些代碼 </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-dfazhfxh-tr.html" target="_blank" title="從源代碼的preg_match"> 從源代碼的preg_match </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-dbrqcpgv-qk.html" target="_blank" title="提取與一些的preg_match HTML"> 提取與一些的preg_match HTML </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-keyrolug-yd.html" target="_blank" title="用PHP收集一些部分HTML(preg_match)"> 用PHP收集一些部分HTML(preg_match) </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-ndmstuep-hc.html" target="_blank" title="Javascript中的preg_match - 從代碼中獲取src(非標準html代碼)"> Javascript中的preg_match - 從代碼中獲取src(非標準html代碼) </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-wizczusf-hr.html" target="_blank" title="從HTML用的preg_match"> 從HTML用的preg_match </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-akdjxofa-bdk.html" target="_blank" title="php代碼跳過一些html內容"> php代碼跳過一些html內容 </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-cxdkkyjp-yd.html" target="_blank" title="使用jquery刪除一些html代碼"> 使用jquery刪除一些html代碼 </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-vopmvqes-hr.html" target="_blank" title="從HTML代碼"> 從HTML代碼 </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-fdgxvhqo-uz.html" target="_blank" title="從HTML代碼"> 從HTML代碼 </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>