dongqiabei7682 2014-01-18 20:17
浏览 14
已采纳

无法匹配某种字符串格式

I am trying to write a preg_match in order to match a given string and I cannot get it right.

What I want to match is this part inside the long text:

Adrian asdzxc

The rule I tried looks like this, but it fails:

preg_match('#<div class="instant_search_title fsl fwb fcb"><a href="(?:[^"]+)">([^<]+)</a>#i', $file_contents, $matches);
print_r($matches);

The long text is this:

<i class="mrs friendsIcon customimg img sp_b6fmvb sx_5e511f"><u>Friend</u></i><span class="uiButtonText">Friends</span></a></div></div><div class="pls"><div class="instant_search_title fsl fwb fcb"><a href="https://www.facebook.com/adrianasdzxc" onclick="if (event.button == 0) &#123; search_logged_ajax(&#123;&quot;ab&quot;:&quot;T_GS_BACKEND_ENTITY_LIMITATION&quot;,&quot;cururl&quot;:&quot;https:\\/\\/www.facebook.com\\/adrianasdzxc&quot;,&quot;fc&quot;:1,&quot;gc&quot;:0,&quot;id&quot;:616226XXX,&quot;init&quot;:&quot;s:unknown&quot;,&quot;is_friend&quot;:true,&quot;is_new_user&quot;:0,&quot;locale&quot;:&quot;en_US&quot;,&quot;o_type&quot;:1,&quot;original_q&quot;:&quot;adriantnt\\u0040yahoo.com&quot;,&quot;q&quot;:&quot;adriantnt\\u0040yahoo.com&quot;,&quot;rank&quot;:0,&quot;rc&quot;:0,&quot;sid&quot;:&quot;10000108681XXXX.3365508868..1&quot;,&quot;start&quot;:0,&quot;typeahead_sid&quot;:null,&quot;u&quot;:&quot;https:\\/\\/www.facebook.com\\/adrianasdzxc&quot;,&quot;t&quot;:&quot;c:name&quot;&#125;);  &#125;" onmouseup="if (event.button != 0) &#123; search_logged_ajax(&#123;&quot;ab&quot;:&quot;T_GS_BACKEND_ENTITY_LIMITATION&quot;,&quot;cururl&quot;:&quot;https:\\/\\/www.facebook.com\\/adrianasdzxc&quot;,&quot;fc&quot;:1,&quot;gc&quot;:0,&quot;id&quot;:616226XXX,&quot;init&quot;:&quot;s:unknown&quot;,&quot;is_friend&quot;:true,&quot;is_new_user&quot;:0,&quot;locale&quot;:&quot;en_US&quot;,&quot;o_type&quot;:1,&quot;original_q&quot;:&quot;adriantnt\\u0040yahoo.com&quot;,&quot;q&quot;:&quot;adriantnt\\u0040yahoo.com&quot;,&quot;rank&quot;:0,&quot;rc&quot;:0,&quot;sid&quot;:&quot;10000108681XXXX.3365508868..1&quot;,&quot;start&quot;:0,&quot;typeahead_sid&quot;:null,&quot;u&quot;:&quot;https:\\/\\/www.facebook.com\\/adrianasdzxc&quot;,&quot;t&quot;:&quot;c:name&quot;&#125;);  &#125;" data-hovercard="/ajax/hovercard/user.php?id=616226XXX">Adrian asdzxc</a></div><div class="fsm fwn fcg"><div class="fbProfileByline searchResultPersonByline"><span class="fbProfileBylineFragment"><span class="fbProfileBylineIconContainer"><i class="mrs fbProfileBylineIcon img sp_9pvis2 sx_897cc1"></i></span><span class="fbProfileBylineLabel">Webmaster at <a href="true"><a href="https://www.facebook.com/pages/Freelancer/640564905962853" data-hovercard="/ajax/hovercard/page.php?id=640564905962853">Freelancer</a></a></span></span><span class="fbProfileBylineFragment"><span class="fbProfileBylineIconContainer"><i class="mrs fbProfileBylineIcon img sp_9pvis2 sx_ed189a"></i></span><span class="fbProfileBylineLabel">Studied Web Design at <a href="true"><a href="https://www.facebook.com/pages/Universitatea-Tibiscus/242668875759811" data-hovercard="/ajax/hovercard/page.php?id=242668875759811">Universitatea Tibiscus</a></a></span></span><span class="fbProfileBylineFragment"><span class="fbProfileBylineIconContainer"><i class="mrs fbProfileBylineIcon img sp_9pvis2 sx_c1d4c8"></i></span><span class="fbProfileBylineLabel">Lives in <a href="true"><a href="https://www.facebook.com/pages/Timi%C8%99oara-poland/107982459236366" data-hovercard="/ajax/hovercard/page.php?id=107982459236366">Timișoara, poland</a></a></span></span></div></div><div><div class="mts detailedsearch_actions"><a href="/browse/mutual_friends/?uid=616226XXX" rel="dialog" ajaxify="/ajax/browser/dialog/mutual_friends/?uid=616226XXX" onclick="if (event.button == 0) &#123; search_logged_ajax(&#123;&quot;ab&quot;:&quot;T_GS_BACKEND_ENTITY_LIMITATION&quot;,&quot;cururl&quot;:&quot;https:\\/\\/www.facebook.com\\/adrianasdzxc&quot;,&quot;fc&quot;:1,&quot;gc&quot;:0,&quot;id&quot;:616226XXX,&quot;init&quot;:&quot;s:unknown&quot;,&quot;is_friend&quot;:true,&quot;is_new_user&quot;:0,&quot;locale&quot;:&quot;en_US&quot;,&quot;o_type&quot;:1,&quot;original_q&quot;:&quot;adriantnt\\u0040yahoo.com&quot;,&quot;q&quot;:&quot;adriantnt\\u0040yahoo.com&quot;,&quot;rank&quot;:0,&quot;rc&quot;:0,&quot;sid&quot;:&quot;10000108681XXXX.3365508868..1&quot;,&quot;start&quot;:0,&quot;typeahead_sid&quot;:null,&quot;u&quot;:&quot;https:\\/\\/www.facebook.com\\/adrianasdzxc&quot;,&quot;t&quot;:&quot;c:mutual_friend&quot;&#125;);  &#125;" onmouseup="if (event.button != 0) &#123; search_logged_ajax(&#123;&quot;ab&quot;:&quot;T_GS_BACKEND_ENTITY_LIMITATION&quot;,&quot;cururl&quot;:&quot;https:\\/\\/www.facebook.com\\/adrianasdzxc&quot;,&quot;fc&quot;:1,&quot;gc&quot;:0,&quot;id&quot;:616226XXX,&quot;init&quot;:&quot;s:unknown&quot;,&quot;is_friend&quot;:true,&quot;is_new_user&quot;:0,&quot;locale&quot;:&quot;en_US&quot;,&quot;o_type&quot;:1,&quot;original_q&quot;:&quot;adriantnt\\u0040yahoo.com&quot;,&quot;q&quot;:&quot;adriantnt\\u0040yahoo.com&quot;,&quot;rank&quot;:0,&quot;rc&quot;:0,&quot;sid&quot;:&quot;10000108681XXXX.3365508868..1&quot;,&quot;start&quot;:0,&quot;typeahead_sid&quot;:null,&quot;u&quot;:&quot;https:\\/\\/www.facebook.com\\/adrianasdzxc&quot;,&quot;t&quot;:&quot;c:mutual_friend&quot;&#125;);  &#125;" role="button">2 mutual friends</a> · <a href="/messages/adrianasdzxc" ajaxify="/ajax/messaging/composer.php?ids%5B0%5D=616226XXX&amp;ref=search" rel="dialog" onclick="if (event.button == 0) &#123; search_logged_ajax(&#123;&quot;ab&quot;:&quot;T_GS_BACKEND_ENTITY_LIMITATION&quot;,&quot;cururl&quot;:&quot;https:\\/\\/www.facebook.com\\/adrianasdzxc&quot;,&quot;fc&quot;:1,&quot;gc&quot;:0,&quot;id&quot;:616226XXX,&quot;init&quot;:&quot;s:unknown&quot;,&quot;is_friend&quot;:true,&quot;is_new_user&quot;:0,&quot;locale&quot;:&quot;en_US&quot;,&quot;o_type&quot;:1,&quot;original_q&quot;:&quot;adriantnt\\u0040yahoo.com&quot;,&quot;q&quot;:&quot;adriantnt\\u0040yahoo.com&quot;,&quot;rank&quot;:0,&quot;rc&quot;:0,&quot;sid&quot;:&quot;10000108681XXXX.3365508868..1&quot;,&quot;start&quot;:0,&quot;typeahead_sid&quot;:null,&quot;u&quot;:&quot;https:\\/\\/www.facebook.com\\/adrianasdzxc&quot;,&quot;t&quot;:&quot;c:action&quot;&#125;);  &#125;" onmouseup="if (event.button != 0) &#123; search_logged_ajax(&#123;&quot;ab&quot;:&quot;T_GS_BACKEND_ENTITY_LIMITATION&quot;,&quot;cururl&quot;:&quot;https:\\/\\/www.facebook.com\\/adrianasdzxc&quot;,&quot;fc&quot;:1,&quot;gc&quot;:0,&quot;id&quot;:616226XXX,&quot;init&quot;:&quot;s:unknown&quot;,&quot;is_friend&quot;:true,&quot;is_new_user&quot;:0,&quot;locale&quot;:&quot;en_US&quot;,&quot;o_type&quot;:1,&quot;original_q&quot;:&quot;adriantnt\\u0040yahoo.com&quot;,&quot;q&quot;:&quot;adriantnt\\u0040yahoo.com&quot;,&quot;rank&quot;:0,&quot;rc&quot;:0,&quot;sid&quot;:&quot;10000108681XXXX.3365508868..1&quot;,&quot;start&quot;:0,&quot;typeahead_sid&quot;:null,&quot;u&quot;:&quot;https:\\/\\/www.facebook.com\\/adrianasdzxc&quot;,&quot;t&quot;:&quot;c:action&quot;&#125;);  &#125;" role="button">Send message</a></div></div></div></div></div></div></div> --></code>
  • 写回答

2条回答 默认 最新

  • dskyx46424 2014-01-18 20:28
    关注

    You can use the DOM:

    $dom = new DOMDocument();
    @$dom->loadHTML($html); //or loadHTMLFile("filename")
    $xpath = new DOMXPath($dom);
    
    $textNode = $xpath->query('//div[contains(@class,"instant_search_title")]/a/text()');
    
    $result = $textNode->item(0)->textContent;
    

    A regex way:

    if (preg_match('~<div class="instant_search_title\b[^<]++<a\b[^>]*+>\K[^<]++~',
                   $html, $match))
        echo $match[0];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动