dongyou8368 2011-03-24 22:59
浏览 19
已采纳

正则表达式模式的简单问题

please help me get the link and text from this tag. <h3 class="post-title entry-title"> has to be included because I want the links from that specific tag.

<h3 class="post-title entry-title">
<a href="http://mymplogk.blogspot.com/2011/03/h_25.html">Text</a>
</h3>

my work so far is

<?php

$string = file_get_contents('http://www.domain.com');

$regex_pattern = "";

unset($matches);
preg_match_all($regex_pattern, $string, $matches);


foreach ($matches[0] as $paragraph) {
echo $paragraph;
echo "<br>";
}
?> 

Thank you in advance

  • 写回答

4条回答 默认 最新

  • duandiao3961 2011-03-24 23:14
    关注

    Following your example, this regex will find "http://mymplogk.blogspot.com/2011/03/h_25.html" and "Text":

    $regex_pattern = '/<h3[^>]+class\s*=\s*[\'"]post-title entry-title[\'"][^>]*>.*?<a[^>]+href\s*=\s*"([^"]+)"[^>]*>([^<]*)</s';
    

    This matches single or double quotes around the h3 tag, and allows additional attributes in h3 tag and optional whitespace between attributes and values. It also matches multiple times in $string, e.g.

    $string = '<h3 class="post-title entry-title">
    <a href="http://mymplogk.blogspot.com/2011/03/h_25.html">Text</a>
    </h3>
    <p>doot</p>
    <h3 class=\'post-title entry-title\'>
    <a href="http://www.google.com/">More Text</a>
    </h3>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?