【发布时间】:2011-01-27 08:12:37
【问题描述】:
您好,我有以下代码,我只想提取带有 www 的链接。在他们里面
<p> Text < href="http://url.com/link/">link text</a> more text < href="http://www.anotehrurl.com/">more link text</a> and < href="http://www.anotherurl.com/sub/sub/link.html">link text</a>. more text < href="http://keepurl.co.uk/link/">link text</a> more text < href="http://www.anotherurl.com/sub/sub/link.html">link text</a>. < href="http://www.anotherurl.com/sub/sub/link.html">link text</a>. < href="http://www.anotehrurl.com/">more link text</a></p>
我使用 reg 表达式:
(<a href="http:\/\/www.[\d\D]*?\/">([\d\D]*?)<\/a>)
基本上我想匹配任何以 www 开头的链接,而不是匹配 keepurl.co.uk/.......的任何链接。
我已经通过 rubular 进行了处理,并得出以下结论:
{ 结果1
1.更多链接文字 2.更多链接文字 结果2
1.链接文字。更多文字 链接文字 2.链接文字 结果 3
1.链接文字。 链接文本。 更多链接文字 2.更多链接文字 }
你可以看到它比我想要的更多。
干杯
【问题讨论】:
-
如何让所有代码正确显示
-
关于我之前的问题,因为所有的 html 和 php 都是可见的,所以一定有人做了一些事情
-
将代码缩进 4 个空格,或者选择它并按下工具栏中的
{}按钮! -
你是解析整个 html 页面,还是仅仅解析一些 sn-ps?也许您应该考虑使用 DOMDocument 或任何其他 php 的 html 解析器?
-
只是片段,我知道可能有更好的方法可以做到这一点,但是现在我的代码已经接近完成,我还没有生命力来重写!