0

I have this regex which is working correctly for urls but when i use a url containing @ it doesnot show anything after @. How can i edit the regex to show full url

    <?php

$regex = "/(https?\:\/\/|\s)[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})(\/+[a-z0-9_.\:\;-]*)*(\?[\&\%\|\+a-z0-9_=,\.\:\;-]*)?([\&\%\|\+&a-z0-9_=,\:\;\.-]*)([\!\#\/\&\%\|\+a-z0-9_=,\:\;\.-]*)}*/i";


$url = "http://www.flickr.com/photos/16506140@N05/8376411748/in/photostream/";

preg_match_all($regex, $url, $matches);

echo'<pre>';
print_r($matches);
echo '<pre>';

?>

DEMO http://codepad.viper-7.com/oVHcdN

Ace
  • 841
  • 9
  • 23
  • 2
    Well if you wrote it then you should know that you must just add some `@` somewhere ... For sake of simplicity, I may use `~https?://\S+~i` – HamZa Oct 28 '13 at 10:03
  • @HamZa copied it from somewhere. I have a very lil knowlege of regex. can u show me regex with added @ & `~https?;//\S+~i` – Ace Oct 28 '13 at 10:06
  • try with this $regex = "/(https?\:\/\/|\s)[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})(\/+[a-z0-9_.\:\;-]*)*(\?[\&\%\|\+a-z0-9_=,\.\:\;-]*)?([\&\%\|\+&a-z0-9_=,\:\;\.-@]*)([\!\#\/\&\%\|\+a-z0-9_=,\:\;\.-]*)}*/i"; – Noman ali abbasi Oct 28 '13 at 10:06
  • @user2894116 anubhava just found a duplicate, you may use it. – HamZa Oct 28 '13 at 10:07
  • @Nomanaliabbasi Quote your code with backticks `\``. – HamZa Oct 28 '13 at 10:07
  • @Nomanaliabbasi Not working http://codepad.viper-7.com/Z6oQNu – Ace Oct 28 '13 at 10:11
  • 2
    Wonder about the discussion here.. Use `parse_url()`, that's all – hek2mgl Oct 28 '13 at 10:11
  • `$regex = "/(https?\:\/\/|\s)[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})(\/+[a-z0-9_.\:\;-]*)*(\?[\&\%\|\+a-z0-9_=,\.\:\;-]*)?([\&\%\|\+&a-z0-9_=,\:\;\.-@]*)([\!\#\/\&\%\|\+a-z0-9_=,\:\;\.-]*)}*/i";` – Noman ali abbasi Oct 28 '13 at 10:13
  • Array ( [0] => Array ( [0] => http://www.flickr.com/photos/16506140@N05/8376411748/in/photostream/ ) [1] => Array ( [0] => http:// ) [2] => Array ( [0] => .flickr ) [3] => Array ( [0] => .com ) [4] => Array ( [0] => /16506140 ) [5] => Array ( [0] => ) [6] => Array ( [0] => @N05/8376411748/in/photostream/ ) [7] => Array ( [0] => ) ) – Noman ali abbasi Oct 28 '13 at 10:13

1 Answers1

3

If you want to access several parts of an url you are highly encouraged to use the function parse_url() instead of a custom regex solution:

$url = "http://www.flickr.com/photos/16506140@N05/8376411748/in/photostream/";
var_dump(parse_url($url));

Output:

array(3) {
  ["scheme"]=>
  string(4) "http"
  ["host"]=>
  string(14) "www.flickr.com"
  ["path"]=>
  string(47) "/photos/16506140@N05/8376411748/in/photostream/"
}
hek2mgl
  • 152,036
  • 28
  • 249
  • 266