从PHP中的字符串中仅获取http URL

时间:2015-01-15 05:55:49

标签: php url preg-match

我一直在尝试从以下字符串中获取URL,但不成功。我怎么能这样做,任何提示?

$string='<b><u>Neale v Commonwealth
Bank of Australia</u></b><b>
[2014] NSWCA 443</b><br>

Court of Appeal of New South Wales<br>

Leeming JA<br>

Appeal - competency - bank was successful judgment creditor in proceedings brought by applicant and his company - bank sought that appeal be dismissed as incompetent or for want of prosecution - requirement that, if well-funded, sophisticated, regular litigant is to object to competency of appeal brought by litigant in person, objection should be made promptly - ability to fund appeal - held: bank had not explained why it did not
make prompt objection - extension of time to seek dismissal of proceedings as incompetent refused - appeal not self-evidently hopeless - severe prejudice ifapplicant denied right of appeal on merits of very substantial judgment - there
had been some explanation for delay and non-compliance with Court procedure -
no particular prejudice to bank - guillotine order made.<br>

<a rel="nofollow" target="_blank" href="http://www.caselaw.nsw.gov.au/action/PJUDG?jgmtid=176362">Neale</a> (B)<br>';

$url=preg_match('/(http:\/\/)(.*)/', $string, $link);
echo $link[0];

输出:http://www.caselaw.nsw.gov.au/action/PJUDG?jgmtid=176362&#34;&gt; Neale(B)

脚本在URL之后添加额外的字符,而不应该在那里。

4 个答案:

答案 0 :(得分:2)

尝试

$url = preg_match('/(http:\/\/)(.*)"/is',$string,$matches);
 echo $matches[2]; // Your answer
你错过了&#39; &#34; &#39;在你的正则表达式中。

答案 1 :(得分:2)

当您从HTML代码中提取它并且您的网址是href属性时,您可以使用

$url=preg_match('/href="([^"]*)"/', $string, $link);
echo $link[1];

答案 2 :(得分:0)

这是正确的正则表达式:

/(HTTP://.+)&#34; /

您可能希望检查返回的数组以检查所需值的确切索引。

答案 3 :(得分:0)

尝试将正则表达式更改为此表达式。

/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i

应该是这样的。

$url = preg_match('/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i', $string, $link);

我希望它会有所帮助。欢呼声。

Source