0

I have been trying to fetch just the URL from the following string but unsuccessful. How can I do that, any tips?

$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];

OUTPUT: http://www.caselaw.nsw.gov.au/action/PJUDG?jgmtid=176362">Neale (B)

The script is adding extra characters after the URL which shouldn't be there.

floCoder
  • 421
  • 1
  • 7
  • 21

4 Answers4

2

Try

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

you missed ' " ' in your regular expression.

Yash
  • 1,446
  • 1
  • 13
  • 26
2

As you are extracting it from a HTML Code and your url is in href attribute, you may use

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

Here is the correct regular expression:

/(http://.+)"/

You might want to inspect the returned array to check the exact index of the value you want.

Noman Ur Rehman
  • 6,707
  • 3
  • 24
  • 39
0

Try to change the you regular expression to this one.

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

It should become like this.

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

I hope it'll help. Cheers.

Source

Adi Utama
  • 98
  • 5