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

Fetch just http URL from the string in PHP

我一直试图从以下字符串中只获取 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">尼尔 (B)

脚本在 URL 之后添加了不应该存在的额外字符。

尝试

$url = preg_match('/(http:\/\/)(.*)"/is',$string,$matches);
 echo $matches[2]; // Your answer

您在正则表达式中遗漏了“””。

正确的正则表达式如下:

/(http://.+)"/

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

尝试将您的正则表达式更改为这个。

/\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

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

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