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.

4
  • try to just capture the href value. and better yet just use an HTML parser (DOMDocument) Commented Jan 15, 2015 at 5:59
  • possible duplicate of PHP validation/regex for URL Commented Jan 15, 2015 at 6:00
  • Its may help you: stackoverflow.com/questions/9151681/… Commented Jan 15, 2015 at 6:20
  • thanks so much for your replies.. i ll follow through them Commented Jan 15, 2015 at 22:19

4 Answers 4

2

Try

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

you missed ' " ' in your regular expression.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.